SICP_3.26

Posted lan126

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

SICP_2.12-2.16(部分习题)

SICP_3.25

SICP_3.27

SICP_3.24

SICP_3.7-3.8

SICP_2.21-2.23