SICP Exercise 4.3
来源:互联网 发布:传奇db数据库参数 编辑:程序博客网 时间:2024/05/23 14:20
Exercise 4.3
在这个练习中,我需要一些对表的操作,下面就是关于表的操作:
(define (make-table) (let ((local-table (list '*table*))) (define (assoc key records) (cond ((null? records) nil) ((equal? key (caar records)) (car records)) (else (assoc key (cdr records))))) (define (lookup key) (assoc key (cdr local-table))) (define (insert! key value) (let ((record (assoc key (cdr local-table)))) (if (not (null? record)) (set-cdr! record value) (set-cdr! local-table (cons (cons key value) (cdr local-table)))))) (define (dispatch m) (cond ((eq? m 'lookup) lookup) ((eq? m 'insert!) insert!) (else (error "Unknown operation -- TABLE" m)))) dispatch))(define (table-get table symbol) ((table 'lookup) symbol))(define (table-put! table symbol value) ((table 'insert!) symbol value))(define (binding-value binding) (cdr binding))
与练习2.73一样,有一些特殊情况不能应用数据导向的分派(sefl-evaluating ,variable,application),因为这些表达式的car部分没有显式的表示它们的类型,所以需要单独处理。对于其他情况,我们可以用分派方式简单处理:
(define (eval exp env) (cond ((self-evaluating? exp) exp) ((variable? exp) (lookup-variable-value exp env)) ((get (car exp)) ((get (car exp)) exp env)) ((application? exp) (apply (eval (operator exp) env) (list-of-values (operands exp) env))) (else (error "Unkown expressioin type -- EVAL" exp))))正如代码所示,用分派方式处理的情况中,这些操作都需要两个参数(exp和env),然后,我们之前的eval并不满足这个条件所以,我们需要自己添加一些包装函数:
(define (eval-quote exp env) (text-of-quotation exp))(define (eval-lambda exp env) (make-procedure (lambda-parameters exp) (lambda-body exp) env))(define (eval-begin exp env) (eval-sequence (begin-actions exp) env))(define (eval-cond exp env) (eval (cond-if exp) env))接下来,就是定义表,以及把过程添加到表中:
(define eval-table (make-table))(define (get type) (let ((binding (table-get eval-table type))) (if (null? binding) #f (binding-value binding))))(define (put type item) (table-put! eval-table type item))
(put 'quote eval-quote)(put 'set! eval-assignment)(put 'define eval-definition)(put 'if eval-if)(put 'lambda eval-lambda)(put 'begin eval-begin)(put 'cond eval-cond)
- SICP Exercise 4.3
- SICP exercise 1.43
- SICP Exercise 3.11
- SICP Exercise 3.15
- SICP Exercise 3.22
- SICP Exercise 3.25
- SICP Exercise 3.28
- SICP Exercise 3.29
- SICP Exercise 3.33
- SICP Exercise 3.34
- SICP Exercise 3.35
- SICP Exercise 3.37
- SICP Exercise 3.38
- SICP Exercise 3.50
- SICP Exercise 3.51
- SICP Exercise 3.52
- SICP Exercise 3.53
- SICP Exercise 3.54
- 书中的一缕阳光_越来越没看看书本清心寡欲的优雅了
- 地址栏图标不对的解决办法
- 多维数据库的数据存储
- 使我困扰的几个C++程序
- J2ME 走向成熟(long long ago)
- SICP Exercise 4.3
- poj 1107W's Cipher
- 关闭Apache的目录浏览功能
- IMX51, SD驱动
- PHP5 VC9、VC6、Thread Safe、Non Thread Safe各个版本区别
- USB HID设备报告描述符详解
- 解决Windows Installer 无法打开此修补程序包的问题
- 四、运算符(例2)
- C语言基础笔记...(待续...)