(define ones (cons-stream 1 ones))
(stream-cdr ones)
returns an infinite sequence of evaluated 1s - i.e. I get
;Value: #0={1 1 1 1 1 1 1 and so on... - not the symbolic {1 1 ...} I would expect ...
On the other end if I define ints and cdr into it,
(define ints (cons-stream 1 (stream-map + ones ints)))
(stream-cdr ints)
I obtain the expected {1 2 ...}
Can anyone explain me why? I expect the stream-map definition to be not too far from
(define (mystream-map proc . argstreams)
(if (stream-null? (car argstreams))
the-empty-stream
(cons-stream
(apply proc (map stream-car argstreams))
(apply mystream-map (cons proc (map stream-cdr argstreams))))))
which I defined in ex 3.50 and returns the same result ... but this seems to (via (map stream-cdr argstreams)) stream-cdr into ones, which I would expect to result in the infinite sequence I get above!
Even though, if I understand correctly, due to cons-stream being a macro the second part inside the stream-map will only be lazily evaluated, at some point while I cdr into the ints stream I would expect to have to cdr into ones as well.. which instead doesn't seem to be happening! :/
Any help in understanding this would be very much appreciated!
(I also didn't override any scheme symbol and I'm running everything in MIT Scheme - Release 11.2 on OS X.)
No it does not.
(stream-cdr ones) = (stream-cdr (cons-stream 1 ones)) = ones. It is just oneones, not the sequence ofones.And what is
ones?Its
stream-caris1, and itsstream-cdrisones.Whose
stream-caris1, andstream-cdrisonesagain.And so if you
(stream-take n ones)(with an obvious implementation ofstream-take) you will receive ann-long list of1s, whatever the (non-negative)nis.In other words, repeated
cdring intoonesproduces an unbounded sequence of1s. Or symbolically,{1 1 1 ...}indeed.After your edit it becomes clear the question is about REPL behavior. What you're seeing most probably has to do with the difference between sharing and recalculation, just like in
letrecwith the true reuse (achieved through self-referring binding) vs. theYcombinator with the recalculation ((Y g) == g (Y g)).You probably can see the behavior you're expecting, with the re-calculating definition
(define (onesF) (cons-stream 1 (onesF))).I only have Racket, where
prints
Furthermore, having defined
we get