码迷,mamicode.com
首页 > 其他好文 > 详细

SICP_3.26

时间:2017-05-14 10:45:52      阅读:327      评论:0      收藏:0      [点我收藏+]

标签:sci   efi   operation   make   target   nts   class   comment   targe   

 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

标签:sci   efi   operation   make   target   nts   class   comment   targe   

原文地址:http://www.cnblogs.com/tclan126/p/6850959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!