chickadee » sicp » stream->list

stream->list streamprocedure
stream->list stream nprocedure

Converts a stream to a list, consuming it (or up to n elements).

stream
The stream to convert to a list
n
Optionally, the maximum number of elements to consume; otherwise: all elements
(define stream->list
  (case-lambda
    ((stream) (stream->list stream +inf.0))
    ((stream n)
     (if (or (stream-null? stream) (zero? n))
       '()
       (cons (stream-car stream)
             (stream->list (stream-cdr stream) (- n 1)))))))