3 (define (stream-car s) (car s))
4 (define (stream-cdr s) (force (cdr s)))
6 (define (stream-ref s n)
9 (stream-ref (stream-cdr s)
12 (define (stream-map proc s)
15 (cons-stream (proc (stream-car s))
16 (stream-map proc (stream-cdr s)))))
18 (define (stream-filter pred? s)
19 (cond [(stream-null? s) the-empty-stream]
20 [(pred? (stream-car s))
21 (cons-stream (stream-car s)
22 (stream-filter pred? (stream-cdr s)))]
23 [else (stream-filter pred? (stream-cdr s))]))
25 (define (stream-for-each proc s)
30 (stream-for-each proc (stream-cdr s)))))
32 (define (display-stream s)
33 (stream-for-each display-line s))
35 (define (display-line x)
39 ;; stream-enumerate-interval
40 (define (stream-enumerate-interval low high)
44 (stream-enumerate-interval (+ low 1)
54 (define seq (stream-map accum (stream-enumerate-interval 1 20)))
56 (define y (stream-filter even? seq))
58 (define z (stream-filter (lambda (x) (= (remainder x 5) 0))
69 What is the value of sum after each of the above expressions is evaluated?
73 > (define seq (stream-map accum (stream-enumerate-interval 1 20)))
76 This is because (stream-enumerate-interval 1 20) builds up a stream
77 which looks as follows:
86 Now, calling stream-map will produce seq which looks like this:
88 seq == (cons (accum (car s)) ;; adds (car s) to sum
89 (delay (stream-map accum (stream-cdr s))))
91 (delay (stream-map accum (stream-cdr s))))
96 (define y (stream-filter even? seq))
98 This will first run (even? (stream-car seq)) which will be false for
99 the first value (i.e. 1). So, it will do:
101 (stream-filter even? (stream-cdr seq))
103 Calling stream-cdr on the seq will 'force' the delayed computation.
107 (stream-map accum (stream-cdr s)))
109 (cons (accum 2) ;; sum becomes 3
110 (delay (stream-map accum (stream-cdr s)))))
112 Now, stream-filter will be called with seq as:
115 (delay (stream-map accum (stream-cdr s))))
117 Again, stream-filter will run the car of seq with the predicate:
121 which will be false; Now the new computation will be:
123 (cons (accum 3) ;; sum becomes 3 + 3 = 6
124 (delay (stream-map accum (stream-cdr s)))))
126 When stream-filter runs the predicate, (even? (stream-car seq))
127 it will return true. At this point, sum = 6.
133 (define z (stream-filter (lambda (x) (= (remainder x 5) 0))
136 At this point, seq is
138 (1 (3 (6 (delay (stream-map accum (stream-cdr s)))))
140 The forming of the new value of z will run it thru 1, 3 and 6
141 and finds that they are not multiple of 5. Now, seq is
143 (cons (accum 4) ;; sum = 6 + 4 = 10
144 (delay (stream-map accum (stream-cdr s))))
148 (delay (stream-map accum (stream-cdr s)))))
150 Now, stream-filter will run the predicate and will return
151 true and returns a stream-cons. At this point, sum = 10.
159 This will take out 7 values out of y, which means 7 even values. So,
160 we produce 16 values in the initial stream before we get 7 even values.
162 (cons 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 (delay (cons 17 (delay (cons 18 ...))))
165 (+ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16)
174 If we were to evaluate seq as a whole and also the rest, we would
179 (1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210)
183 (10 28 36 66 78 120 136 190 210)
187 (10 15 45 55 95 105 120 190 210)