Skip to content
Browse files

add: 3.25

  • Loading branch information...
1 parent 52d309d commit 462db288faae9483c77b31cc6f467be5a75c5e25 @qiao committed Dec 2, 2011
Showing with 33 additions and 0 deletions.
  1. +33 −0 chapter3/3.25.scm
View
33 chapter3/3.25.scm
@@ -0,0 +1,33 @@
+;; simply use ``equal?'' to test keys
+(define (make-table)
+ (define (assoc key records)
+ (cond ((null? records) false)
+ ((equal? key (caar records)) (car records))
+ (else (assoc key (cdr records)))))
+ (let ((local-table (list '*table*)))
+ (define (lookup key-1 key-2)
+ (let ((subtable (assoc key-1 (cdr local-table))))
+ (if subtable
+ (let ((record (assoc key-2 (cdr subtable))))
+ (if record
+ (cdr record)
+ false))
+ false)))
+ (define (insert! key-1 key-2 value)
+ (let ((subtable (assoc key-1 (cdr local-table))))
+ (if subtable
+ (let ((record (assoc key-2 (cdr subtable))))
+ (if record
+ (set-cdr! record value)
+ (set-cdr! subtable
+ (cons (cons key-2 value)
+ (cdr subtable)))))
+ (set-cdr! local-table
+ (cons (list key-1
+ (cons key-2 value))
+ (cdr local-table))))))
+ (define (dispatch m)
+ (cond ((eq? m 'lookup-proc) lookup)
+ ((eq? m 'insert-proc) insert!)
+ (else (error "Unkown operation -- TABLE" m))))
+ dispatch))

0 comments on commit 462db28

Please sign in to comment.
Something went wrong with that request. Please try again.