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

【SICP练习】147 练习4.3

时间:2015-03-30 09:33:38      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:car   rewrite   eval   compare   data   

练习4-3

原文

Exercise 4.3. Rewrite eval so that the dispatch is done in data-directed style. Compare this with the datadirected differentiation procedure of exercise 2.73. (You may use the car of a compound expression as the type of the expression, as is appropriate for the syntax implemented in this section.) .

分析

参照练习2.73即可。

 (define operation-table make-table) 
 (define get (operation-table ‘lookup-proc)) 
 (define put (operation-table ‘insert-proc)) 

 (put ‘op ‘quote text-of-quotation) 
 (put ‘op ‘set! eval-assignment) 
 (put ‘op ‘define eval-definition) 
 (put ‘op ‘if eval-if) 
 (put ‘op ‘lambda (lambda (x y) (make-procedure (lambda-parameters x) (lambda-body x) y))) 
 (put ‘op ‘begin (lambda (x y) (eval-sequence (begin-sequence x) y))) 
 (put ‘op ‘cond (lambda (x y) (evaln (cond->if x) y))) 

 (define (evaln expr env) 
         (cond ((self-evaluating? expr) expr) 
                   ((variable? expr) (lookup-variable-value expr env)) 
                   ((get ‘op (car expr)) (applyn (get ‘op (car expr) expr env))) 
                   ((application? expr) (applyn (evaln (operator expr) env) (list-of-values (operands expr) env))) 
                   (else  
                    (error "Unkown expression type -- EVAL" expr)))) 

【SICP练习】147 练习4.3

标签:car   rewrite   eval   compare   data   

原文地址:http://blog.csdn.net/nomasp/article/details/44729561

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