首页 > 代码库 > SICP_3.26
SICP_3.26
1 (define false #f) 2 (define true #t) 3 4 (define (make-table) 5 (let ((local-table ‘())) 6 7 (define (make-tree key value left-branch right-branch) 8 (list (cons key value) left-branch right-branch)) 9 10 (define (record-key tree) 11 (caar tree)) 12 13 (define (record-value) 14 (cdar tree)) 15 16 (define (left-sub tree) 17 (cadr tree)) 18 19 (define (right-sub tree) 20 (caddr tree)) 21 22 (define (record tree) 23 (car tree)) 24 25 (define (lookup key) 26 (define (lookup-helper tree) 27 (if (null? tree) 28 false 29 (if (= (record-key tree) key) 30 (record-value tree) 31 (if (< (record-key tree) key) 32 (lookup-helper (left-sub tree)) 33 (lookup-helper (right-sub tree)))))) 34 (lookup-helper local-table)) 35 36 (define (insert! key value) 37 (define (insert-helper! tree) 38 (if (null? tree) 39 (set! tree (make-tree key value ‘() ‘())) 40 (if (= (record-key tree) key) 41 (set-cdr! (record tree) value) 42 (if (< (record-key tree) key) 43 (insert-helper! (left-sub tree)) 44 (insert-helper! (right-sub tree)))))) 45 (insert-helper! local-table) 46 ‘ok) 47 48 (define (print-table) 49 (display local-table) 50 (newline)) 51 52 (define (dispatch m) 53 (cond ((eq? m ‘print-table) print-table) 54 ((eq? m ‘insert!) insert!) 55 ((eq? m ‘lookup) lookup) 56 (else (error "Unknow operation --TABLE" m)))) 57 58 dispatch)) 59 60 (define t1 (make-table)) 61 ((t1 ‘print-table)) 62 ((t1 ‘lookup) 4) 63 ((t1 ‘insert!) 4 ‘lan) 64 ((t1 ‘insert!) 5 ‘tian) 65 ((t1 ‘print-table))
insert! 部分有问题明明set! 了 local-table 却没有什么效果,可能是我对环境模型的理解不够导致的。 参考
SICP_3.26
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。