learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-24.scm (1197B)


      1 (define-library (sicp solutions 3.24)
      2   (import (scheme base))
      3 
      4   (begin
      5     (define (my-assoc same-key? key table)
      6       (if (null? table)
      7           #f
      8           (let ((record (car table)))
      9             (if (same-key? key (car record))
     10                 record
     11                 #f))))
     12 
     13     (define (make-table same-key?)
     14       (let ((local-table (list '*table*)))
     15         (define (lookup key)
     16           (let ((record (my-assoc same-key?
     17                                   key
     18                                   local-table)))
     19             (if record
     20                 record
     21                 #f)))
     22 
     23         (define (insert! key value)
     24           (let ((record (my-assoc same-key?
     25                                   key
     26                                   table)))
     27             (if record
     28                 (set-cdr! record
     29                           value)
     30                 (set-cdr! local-table
     31                           (cons (cons key value)
     32                                 (cdr local-table)))))
     33 
     34           'ok)
     35 
     36         (define (dispatch m)
     37           (cond
     38            ((eq? m 'lookup) lookup)
     39            ((eq? m 'insert!) insert!)
     40            (else (error "Unknown operation TABLE: " m))))
     41 
     42         dispatch))))