首页 > 其他 > 详细

【SICP练习】148 练习4.4

时间:2015-03-31 14:45:42      阅读:137      评论:0      收藏:0      [点我收藏+]

练习4-4

原文

Exercise 4.4. Recall the definitions of the special forms and and or from chapter 1:

● and: The expressions are evaluated from left to right. If any expression evaluates to false, false is returned; any remaining expressions are not evaluated. If all the expressions evaluate to true values, the value of the last expression is returned. If there are no expressions then true is returned.

● or: The expressions are evaluated from left to right. If any expression evaluates to a true value, that value is returned; any remaining expressions are not evaluated. If all expressions evaluate to false, or if there are no expressions, then false is returned.

Install and and or as new special forms for the evaluator by defining appropriate syntax procedures and evaluation procedures eval-and and eval-or. Alternatively, show how to implement and and or as derived expressions.

代码

 ((and? expr) (evaln (and->if expr) env)) 

 (define (and->if expr) 
         (expand-and-clauses (and-clauses expr))) 
 (define (expand-and-clauses clauses) 
         (if (null? clauses) 
             (make-iftruetruefalse)         
                 (let ((first (car clauses)) 
                           (rest (cdr clauses))) 
                    (if (null? rest)  
                            (make-if first first ‘false) 
                        (make-if first (expand-and-clauses rest)false))))) 

 ((or? expr) (evaln (or->if expr) env))  
 (define (or->if expr) 
         (expand-or-clauses (or-clauses expr))) 
 (define (expand-or-clauses clauses) 
         (if (null? clauses) 
             (make-iftruefalsetrue) 
                 (let ((first (car clauses)) 
                           (rest (cdr clauses))) 
                  (make-if first ‘true (expand-or-clauses rest))))) 

【SICP练习】148 练习4.4

原文:http://blog.csdn.net/nomasp/article/details/44778307

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!