码迷,mamicode.com
首页 > 其他好文 > 详细

【SICP练习】143 练习3.81

时间:2015-03-29 16:37:04      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

练习3-81

原文

“random” numbers. Produce a stream formulation of this same generator that operates on an input stream of requests to generate a new random number or to reset the sequence to a specified value and that produces the desired stream of random numbers. Don’t use assignment in your solution.Exercise 3.81. Exercise 3.6 discussed generalizing the random-number generator to allow one to reset the random-number sequence so as to produce repeatable sequences of

代码


(define (random-update x)  
   (remainder (+ (* 13 x) 5) 24))
;Value: random-update

(define random-init 
   (random-update (expt 2 32)))
;Value: random-init

(define (random-number-generator command-stream) 
   (define random-number  
       (cons-stream random-init    
                    (stream-map (lambda (number command)            
                                (cond    
                                 ((null? command) the-empty-stream)     
                                 ((eq? command ‘generator)       
                                  (random-update number))    
                                ((and (pair? command)              
                                      (eq? (car command) ‘reset))    
                                 (cdr command))      
                                (else     
                                 (error "bad command -- " command))))        
                                random-number        
                                command-stream))) 
       random-number)
 ;Value: random-number-generator

【SICP练习】143 练习3.81

标签:

原文地址:http://blog.csdn.net/nomasp/article/details/44727497

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!