learning-sicp

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

exercise-26.scm (1224B)


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