Stream of random numbers

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 ''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.

There are no comments yet.

Authentication required

You must log in to post a comment.

(define (stream-car stream) (car stream))

(define (stream-cdr stream) (force (cdr stream)))

(define (stream-map proc . list-of-stream)
    (if (null? (car list-of-stream))
            (apply proc 
                   (map (lambda (s)
                            (stream-car s))
            (apply stream-map 
                   (cons proc (map (lambda (s)
                                       (stream-cdr s))

(define (stream-ref s n)
  (if (= n 0)
      (stream-car s)
      (stream-ref (stream-cdr s) (- n 1))))

(define test-stream (cons-stream 'generate
                    (cons-stream 'generate
                    (cons-stream 'generate '()))))

(define s0 (random-numbers test-stream))

(define test-reset-stream (cons-stream (stream-ref s0 0)
                          (cons-stream 'generate
                          (cons-stream 'generate
                          (cons-stream 'generate
                          (cons-stream 'generate '()))))))

(define s1 (random-numbers test-reset-stream))

(check-equal? (stream-ref s0 0) (stream-ref s1 0))
(check-equal? (stream-ref s0 0) (stream-ref s1 1))
(check-equal? (stream-ref s0 1) (stream-ref s1 2))
(check-equal? (stream-ref s0 2) (stream-ref s1 3))