-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathe-3.81.scm
61 lines (52 loc) · 2.18 KB
/
e-3.81.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
; 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 ``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.
; ------------------------------------------------------------
; in exercise 3.6 it is defined as a object with the internal state.
(load "3.5.scm")
(load "../3.1/e-3.6.scm")
; first initializer
(define random-init
(inexact->exact (current-milliseconds)))
; lets say random update is this one
(define random-update
(let ((m 19)
(a 3)
(c 5))
(lambda (x)
(modulo (+ (* a x) c) m))))
; stream of random numbers is defined
(define random-numbers
(cons-stream random-init
(stream-map rand-update random-numbers)))
; what we want to achieve is to have stream of random numbers which has
; argument the other stream in which is placed whether to generate new
; one or to start from the new value
; directed-pseudo-random-stream
(define (dp-random-stream directions)
(cons-stream
random-init
(stream-map
(lambda (number direction)
(if (eq? direction 'generate)
(random-update number)
direction))
(dp-random-stream directions) ; self-reference to the stream
directions)))
; test stream directions
(define directions-stream
(cons-stream 'generate
(cons-stream 'generate
(cons-stream 0
(cons-stream 'generate
(cons-stream 'generate
(cons-stream 'generate
(cons-stream 'generate directions-stream))))))))
(define test (dp-random-stream directions-stream))
(display-stream-head test 20); with randomly inited first value -> 959 13 6 0 5 1 8 10 16 15 0 5 1 8 10 16 15 0 5 1