"SICP Exercise" 148 Exercise 4.4

Source: Internet
Author: User

Exercise 4-4 Original

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

And:the expressions is evaluated from left to right. If any expression evaluates to False, False is returned; Any remaining expressions is not evaluated. If all the expressions evaluate to true values, the value of the last expression is returned. If there is no expressions then true is returned.

Or:the expressions is evaluated from left to right. If any expression evaluates to a true value, which value is returned; Any remaining expressions is not evaluated. If all expressions evaluate to false, or if there be no expressions, then false is returned.

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

Code
 ((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-if 'true 'true 'false)                          ( 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-if 'true 'false 'true)                  ( let ( car clauses)(rest (  CDR clauses ))(make-if first 'true (expand-or-clauses rest                    ))))) 

"SICP Exercise" 148 Exercise 4.4

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.