[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: some suggestions



On Thursday, February 13, 2003 11:26 AM, Matthias Neubauer [SMTP:neubauer@xxxxxxxxxxxxxxxxxxxxxxxxxx] wrote:
> Phil Bewig <pbewig@xxxxxxxxxx> writes:
> 
> > Certainly NULL, CONS, CAR, CDR, NULL?, PAIR? and LAMBDA must be
> > in the core, since they all necessarily touch the underlying representation of
> > streams.  You probably want STREAM to build finite streams and ITERATE
> > to build infinite streams, and REF to access items other than the first.
> > MAP, FOR-EACH, FILTER and FOLD-LEFT are useful in their own right and
> > also as building blocks for numerous other functions.  That's fourteen.  Does
> > that make a useful core?
> 
> To my mind, the most intrinsic stream abstraction ever is still
> missing in this srfi: UNFOLDR. UNFOLDR is for streams, what FOLDR is
> for lists. Whereas it is a highly common task to consume a list to a
> single value using a consumer function---typically done by using
> FOLDR---, the common pattern for streams is generating a stream
> starting from an initial seed value using a generator function.
> 
> And that's what UNFOLDR is all about!
> 
> So I vote for putting UNFOLDR in the core srfi:
> (Be warned: the following code is completely untested und unoptimized!)
> 
> ;; stream-unfold : (b -> (union (cons a b) #f)) b -> stream(a)
> (stream-define (stream-unfold gen-fun seed)
>    (let ((res (gen-fun seed)))
>      (if res
>          (stream-cons (car res)
>                       (stream-unfold gen-fun (cdr res)))
>          stream-null)))

I think the STREAM-ITERATE in the core does exactly what
your STREAM-UNFOLD does, and certainly your STREAM-FROM
and STREAM-FROM-TO examples can be written based on
STREAM-ITERATE.  And, given that STREAM is in the core, your
LIST->STREAM can be written

    (define (list->stream lst) (apply stream lst))

Phil