Next: , Previous: , Up: Data structures   [Contents][Index]

14.7 Streams - lazy lists

Streams, sometimes called lazy lists, are a sequential data structure containing elements computed only on demand. A stream is either null or is a pair with a stream in its cdr. Since elements of a stream are computed only when accessed, streams can be infinite. Once computed, the value of a stream element is cached in case it is needed again.

Note: These are not the same as Java 8 streams.

(require 'srfi-41)
(define fibs
  (stream-cons 1
    (stream-cons 1
      (stream-map +
        (stream-cdr fibs)))))
(stream->list 8 fibs) ⇒ (1 1 2 3 5 8 13 21)

See the SRFI 41 specification for details.

The Kawa implementations builds on promises. The stream-null value is a promise that evaluates to the empty list. The result of stream-cons is an eager immutable pair whose car and cdr properties return promises.