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