3.81 Exercise 3.81
Once we’ve defined a procedure that takes the current message and the last
random value generated, we can trivially define a random-number generating
stream by using stream-map on the stream of messages as well as the
stream itself. (Note that the message is used as the first argument to this
procedure – because the definition of stream-map I’m using only checks
if the first stream is out of arguments, we need to use this stream because
it will have one fewer element.)
(define (rand-gen msgs) | (define (next-value msg last) | (cond ((eq? msg 'generate) (rand-update last)) | ((and (pair? msg) (eq? (car msg) 'reset)) (cdr msg)) | (else (error "Invalid message -- RAND-GEN" msg)))) | (define s (cons-stream random-init (stream-map next-value msgs s))) | s) |
|