【SICP练习】145 练习4.1-阿里云开发者社区

开发者社区> 云计算> 正文

【SICP练习】145 练习4.1

简介: 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/44728049 练习4-1 原文 Exercise 4.
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/44728049

练习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))))) 



感谢您的访问,希望对您有所帮助。 欢迎大家关注或收藏、评论或点赞。


为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

其他文章