sicp-ex-3.6


 (define rand 
   (let ((x random-init)) 
     (define (dispatch message) 
       (cond ((eq? message 'generate) 
               (begin (set! x (rand-update x)) 
                      x)) 
             ((eq? message 'reset) 
               (lambda (new-value) (set! x new-value))))) 
     dispatch)) 

Test:

 (define random-init 0) 
 (define (rand-update x) (+ x 1)) ; A not-very-evolved PNRG 
 (rand 'generate) 
 ; 1 
 (rand 'generate) 
 ; 2 
 ((rand 'reset) 0) 
 ; 0 
 (rand 'generate) 
 ; 1 

It's interesting to notice that the lambda returned by a call to (rand 'reset) still has the closure we created as lexical scope:

 x 
 ; Error: undefined variable 'x'. 

<< Previous exercise (3.5) | Index | Next exercise (3.7) >>