Exercise 4-5 Original
Exercise 4.5. Scheme allows an additional syntax for cond clauses, (= =). If evaluates to a true value, then is evaluated. Its value must is a procedure of one argument; This procedure was then invoked on the value of the, and the result is returned as the value of the the COND expression. For example
(cond ((assoc ‘b ‘((a 1) (b 2))) => cadr) (else false))
Returns 2. Modify the handling of cond so, it supports this extended syntax.
Analysis
Code
(define (extended-cond-syntax clause) (eq? ) (cadr clause) ' =]) (define (extended-cond-test clause) (car clause)) (define (extended-cond-recipient clause) (caddr clause) ) (define (cond->if expr)(expand-clauses (cond-clauses Expr ))) (Define (expand-clauses clauses) (if (null? clauses)‘false ( Let (First (car clauses))(rest (cdr clauses) )) (Cond cond-else-clause? first) (if (null? rest) ( sequence->exp cond-actions first) ) (error "ELSE clause isn ' t last-- Cond->if " clauses) ) ) extended-cond-syntax? first) (make-if (extended-cond-test First" (list (extended-cond-recipient first) extended-cond-test first) ) (expand-clauses rest) ) ) (else (make-if (cond-predicate first) (sequence->exp (cond-actions first)) (expand-clauses rest)))))))
"SICP Exercise" 149 Exercise 4.5