sicp-ex-4.32



<< Previous exercise (4.31) | Index | Next exercise (4.33) >>


meteorgan

  
  
  
 In chapter 3, the car is not lazy. but here car and cdr are all lazy-evaluated. then we can build a lazy tree, all the branches of the tree are lazy-evaluated. 

Sphinxsky

  
  
  
 ; different examples 
 ; in the normal interpreter, "this is car" is printed 
 (define show-1 
     (cons-stream 
         (begin 
             (display 'this-is-car) 
             'this-is-car) 
         (begin 
             (display 'this-is-cdr) 
             'this-is-cdr))) 
  
 ; in the lazy evaluation interpreter, it is not 
 (define show-2 
     (cons 
         (begin 
             (display 'this-is-car) 
             'this-is-car) 
         (begin 
             (display 'this-is-cdr) 
             'this-is-cdr))) 
  
  
 ; examples of applications 
 ; in lazy evaluators, you don't have to worry about the order of definitions 
  
 (define infinite-matrix-ones (cons ones infinite-matrix-ones)) 
  
 (define ones (cons 1 ones)) 
  
 (define (matrix-ref matrix x y) (list-ref (list-ref matrix x) y)) 
  
 (matrix-ref infinite-matrix-ones 5011 10)