(define sum 0)
(define (accum x)
(set! sum (+ x sum))
sum)
;1: (define seq (stream-map accum (stream-enumerate-interval 1 20)))
;2: (define y (stream-filter even? seq))
;3: (define z (stream-filter (lambda (x) (= (remainder x 5) 0))
; seq))
;4: (stream-ref y 7)
;5: (display-stream z)
Step 1:
;1: ==> (cons-stream 1 (stream-map proc (stream-cdr s))
(Assume stream-cdr
is evaluated only when we force the cdr
of this stream)
sum
is now 1
Step 2:
1
is not even, hence (also memoized so not added again), it calls (stream-filter pred (stream-cdr stream))
.
This leads to
evaluation of cdr
hence materializing 2
which is even, hence it should call: (cons-stream 2 (stream-cdr stream))
.
According to this answer should be 1+2 = 3 , but it is 6
Can someone help with why the cdr
's car
is materialized before the current cdr
is called?