SICP chapter2 put get

来源:互联网 发布:今天淘宝怎么进不去了 编辑:程序博客网 时间:2024/06/08 05:11
自己在做的时候挺不方便的附上put get代码帮助需要的朋友们
(define (make-table) (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)))))'OK)(define (dispatch m)(cond ((eq? m 'lookup-proc) lookup)((eq? m 'insert-proc) insert!)(else (error "Unknown operation -- TABLE" m))))dispatch))(define operation-table (make-table))(define get (operation-table 'lookup-proc))(define put (operation-table 'insert-proc))
<img src="http://img.blog.csdn.net/20160523123922759?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
0 0
原创粉丝点击