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

SICP_3.25

时间:2017-05-08 01:19:57      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:lookup   error   blog   records   sub   color   car   sso   sicp   

 1 (define false #f)
 2 (define true #t)
 3 
 4 (define (make-table)
 5   (let ((local-table (list *table*)))
 6 
 7      (define (assoc key records)
 8       (cond ((null? records) false)
 9             ((equal? (caar records) key) (car records))
10             (else (assoc key (cdr records)))))
11 
12     (define (lookup keys)
13       (define (lookup-helper keys table)
14         (let ((subtable (assoc (car keys) (cdr table))))
15           (if subtable
16               (if (null? (cdr keys))
17                   (cdr subtable)
18                   (lookup-helper (cdr keys) subtable))
19               false)))
20       (lookup-helper keys local-table))
21 
22     (define (insert! keys value)
23       (define (insert-helper! keys table)
24         (if (null? table)
25             (if (null? (cdr keys))
26                 (cons (car keys) value)
27                 (list (car keys) (insert-helper! (cdr keys) ())))
28             (let ((sub (assoc (car keys) (cdr table))))
29               (if sub
30                   (if (null? (cdr keys))
31                       (set-cdr! sub value)
32                       (insert-helper! (cdr keys) sub))
33                   (if (null? (cdr keys))
34                       (set-cdr! table (cons (cons (car keys) value) (cdr table)))
35                       (set-cdr! table (cons
36                                        (list (car keys)(insert-helper! (cdr keys) ()))
37                                        (cdr table))))))))
38       (insert-helper! keys local-table)
39       ok)
40   
41     (define (dispatch m)
42       (cond ((eq? m lookup-proc) lookup)
43             ((eq? m insert-proc) insert!)
44             (else (error "Unknow operation --TABLE" m))))
45 
46     dispatch))
47 
48 (define t1 (make-table))
49 
50 ((t1 insert-proc) (key1 key2 key4) 5)
51 
52 ((t1 lookup-proc) (key1 key2 key4))

 

SICP_3.25

标签:lookup   error   blog   records   sub   color   car   sso   sicp   

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

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