[ prog / sol / mona ]

prog


[challenge] Try to rewrite SICP [urgent]

7 2022-08-16 07:12

>>5
Scheme driver for this:

(define (count guess)
  (with-input-from-file "sicp.md"
    (lambda ()
      (let loop ((prev #f)
                 (score 0))
        (let ((current (read-char)))
          (cond
           ((eof-object? current) score)
           (else (loop current (if (eq? (guess prev) current)
                                   (+ score 1)
                                   score)))))))))

Some values:

(count (lambda (x) #\)))
;; 14799
(count (lambda (x) #\())
;; 14799

;; Try matching open parentheses
(define (matcher)
  (let ((depth 0))
    (lambda (prev)
      (when (eq? prev #\()
        (set! depth (+ depth 1)))
      (when (eq? prev #\))
        (set! depth (- depth 1)))
      (cond
       ((> depth 0) #\))
       (else #\()))))
;; 19535

(count (lambda (x) #\space))
;; 215505

;; Conclusion: SICP is mostly empty.
14


VIP:

do not edit these