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

【SICP练习】145 练习4.1

时间:2015-03-29 18:08:21      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

练习4-1

原文

Exercise 4.1. Notice that we cannot tell whether the metacircular evaluator evaluates operands from left to right or from right to left. Its evaluation order is inherited from the underlying Lisp: If the arguments to cons in list-of-values are evaluated from left to right, then list-of-values will evaluate operands from left to right; and if the arguments to cons are evaluated from right to left, then list-ofvalues will evaluate operands from right to left.Write a version of list-of-values that evaluates operands from left to right regardless of the order of evaluation in the underlying Lisp. Also write a version of list-of-values that evaluates operands from right to left.

分析

如题中所说的,list-of-values的求值顺序由cons的参数而定。同时也要使用no-operands?来判断exps,如果为真则返回空。而左右之分,在let中的求值顺序可以决定。如果是从左到右就应该是先求值first而后求值rest,最后用cons来构造到一起即可。

代码

 (define (list-of-values-l-to-r exps env) 
   (if (no-operands? exps)() 
       (let ((first (eval (first-operand exps) env))) 
         (let ((rest (list-of-values-l-to-r (rest-operands exps) env))) 
           (cons first rest))))) 

 (define (list-of-values-r-to-l exps env) 
   (if (no-operands? exps)() 
       (let ((rest (list-of-values-r-to-l (rest-operands exps) env))) 
         (let ((first (eval (first-operand exps) env))) 
           (cons first rest))))) 

【SICP练习】145 练习4.1

标签:

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

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