chickadee » srfi-40 » stream-unfoldn

stream-unfoldn generator seed nprocedure

stream-unfoldn returns n streams whose contents are produced by successive calls to generator, which takes the current seed as an arguments and returns n + 1 values:

    (proc seed) -> seed result0 ... resultN

where resultI indicates how to produce the next element of the Ith result stream:

value is the next car of this result stream
no new information for this result stream
the end of this result stream has been reached

Note that getting the next element in any particular result stream may require multiple calls to generator.

(define (take5 s)
    (lambda (x)
      (let ((n (car x)) (s (cdr x)))
        (if (zero? n)
            (values 'dummy '())
              (cons (- n 1) (stream-cdr s))
              (list (stream-car s))))))
    (cons 5 s)
(take5 from0)                              => (stream 0 1 2 3 4)