sicp-ex-4.26



<< Previous exercise (4.25) | Index | Next exercise (4.27) >>


meteorgan

  
  
 ;; add this code in eval 
 ((unless? expr) (eval (unless->if expr) env)) 
  
 ;; unless expression is very similar to if expression. 
  
 (define (unless? expr) (tagged-list? expr 'unless)) 
 (define (unless-predicate expr) (cadr expr)) 
 (define (unless-consequnce expr) 
   (if (not (null? (cdddr expr))) 
       (cadddr expr) 
       'false)) 
 (define (unless-alternative expr) (caddr expr)) 
  
 (define (unless->if expr) 
   (make-if (unless-predicate expr) (unless-consequence expr) (unless-alternative expr))) 

First I should note that my implementation (same as meteorgan's above) does not even work for the lazy factorial procedure...it aborts due to infinite recursion. As for actual higher order procedures here's a very clever example borrowed from Xueqiao Xu, binary pattern matching:

  
 (define select-y '(#t #f #t #t)) 
 (define xs '(1 3 5 7)) 
 (define ys '(2 4 6 8)) 
 (define selected (map unless select-y xs ys)) 
  

It actually works well with the factorial procedure in my implementation.