# specification of STREAM-UNFOLDN generators

This page is part of the web mail archives of SRFI 40 from before July 7th, 2015. The new archives for SRFI 40 contain all messages, not just those from before July 7th, 2015.

```Hi Phil,

I really like your new draft! Looks awesome! :-)

I found something that looks counterintuitive to me (or it is just me
who can't see the rationale of the current specification ... :-).

The type of the generator of STREAM-UNFOLDN is now specified as
follows:

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

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

(value)  -> value is the next car of this result stream
#f       -> no new information for this result stream
()       -> the end of this result stream has been reached

Wouldn't it be more "natural" to swap the result types? Something like
this:

(v)      -> v is the next car of this result stream
()       -> no new information for this result stream
#f       -> the end of this result stream has been reached

Then, if a list is returned (case 1 and 2), it is just like this new
list gets "prepended" to the result stream. Additionally, #f feels
more like signaling "the end" to me ... :-)

I would then even consider to make the results of the generators more
general. Now, you're only allowed to prepend zero or one new element
to the beginning of the result stream. Why not two, three, or more?
The generalization would also simplify the specification:

(v0 ... vn) -> v0 ... vn is the beginning of this result stream
#f          -> the end of this result stream has been reached

Here is a little toy function FOO demonstrating this "feature": given
a stream of pairs of ints and something else, e.g.

((3 . a) (4 . b) (0 . x) (42 . y) ...),

FOO generates a new stream, repeating the "cdrs" of the pairs
according to the count in their "cars". For the above stream the
result would be

(a a a b b b b y y y y y y ...).

Using the new interface FOO can be written as follows:

(define (foo s)
(stream-unfoldn
(lambda (seed)
(if (stream-null? seed)
(values 'dummy #f)
(values
(stream-cdr seed)
(let ((h (stream-car seed)))
(make-list (car h) (cdr h))))))
s
1))

The advantage of the new interface is obvious: you don't have to
handle the empty list separately, and adding the new elements to the
beginning of the result stream is done in one go.

-Matthias

--
Matthias Neubauer                                       |
Universität Freiburg, Institut für Informatik           | tel +49 761 203 8060
Georges-Köhler-Allee 79, 79110 Freiburg i. Br., Germany | fax +49 761 203 8052

```