[racket] stream-cons from racket/stream isn't lazy

From: Eugene Toder (eltoder at gmail.com)
Date: Sun Mar 6 14:47:29 EST 2011

To clarify the last point. Say each seq has a method to create an
equivalent memoized seq, or return itself if memoization is not needed
(e.g. in-range). Memoized seq returns a memoizing iterator, that first
goes over previously calculated values, then starts storing newly
calculated values in seq as it calculates them. Interface for this is
a function stream-memoize :: seq -> seq. So

(let ([s2 (stream-map func s)])
  (for ([i s2]) ...)
  (for ([i s2]) ...))

calculates all values twice (but does not use additional memory), but

(let ([s2 (stream-memoize (stream-map func s))])
  (for ([i s2]) ...)
  (for ([i s2]) ...))

only calculates values once.

Eugene


Posted on the users mailing list.