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

【SICP练习】146 练习4.2

时间:2015-03-30 09:35:28      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:for   before   eval   applications   cond   

练习4-2

原文

Exercise 4.2. Louis Reasoner plans to reorder the cond clauses in eval so that the clause for procedure applications appears before the clause for assignments. He argues that this will make the interpreter more efficient: Since programs usually contain more applications than assignments, definitions, and so on, his modified eval will usually check fewer clauses than the original eval before identifying the type of an expression.
a. What is wrong with Louis’s plan? (Hint: What will Louis’s evaluator do with the expression (define x 3)?)
b. Louis is upset that his plan didn’t work. He is willing to go to any lengths to make his evaluator recognize procedure applications before it checks for most other kinds of expressions. Help him by changing the syntax of the evaluated language so that procedure applications start with call. For example, instead of (factorial 3) we will now have to write (call factorial 3) and instead of (+ 1 2) we will have to write (call + 1 2).

分析

所有的序对都被当作应用。
因为都是以call开始,所以先用cdr取出exp,再分别用car和cdr取出operator和operands即可。

 (define (application? exp) (tagged-list? exp ‘call)) 
 (define (operator exp) (cadr exp)) 
 (define (operands exp) (cddr exp)) 

【SICP练习】146 练习4.2

标签:for   before   eval   applications   cond   

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

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