[plt-scheme] how do you use stream-partition?

From: Doug Orleans (dougorleans at gmail.com)
Date: Sun Mar 16 17:28:27 EDT 2008

Jos Koot writes:
 > Therefore both streams must have state that can can be modified by
 > both.

The author of SRFIs 40 and 41 sent me the following message (forwarded
with permission).  Seems like there's no need for mutable state to
implement stream-partition.  (By the way, are there plans to
incorporate SRFI-41 into PLT?  Note that stream-partition is not
actually provided by SRFI-41, it's just an example from the docs, so
there's still a need for a utility package like stream.plt.)

--dougorleans at gmail.com

------- start of forwarded message -------
From: "Phil Bewig" <pbewig at gmail.com>
To: "Doug Orleans" <dougorleans at gmail.com>
Subject: Re: [plt-scheme] how do you use stream-partition?
Date: Sat, 15 Mar 2008 12:26:44 -0500

SRFI-40 is deprecated in favor of SRFI-41.  SRFI-41 gives stream-partition
as an example, in the discussion of stream-unfolds, and shows how to use the
two streams that it returns.  Quoting SRFI-41:

Stream-unfolds is especially useful when writing expressions that return
multiple streams. For instance, (stream-partition *pred?* *strm*) is
equivalent to

  (stream-filter *pred?* *strm*)
    (lambda (x) (not (*pred?* x))) *strm*))

but only tests * pred?* once for each element of *strm*.

(define (stream-partition pred? strm)
    (lambda (s)
      (if (stream-null? s)
          (values s '() '())
          (let ((a (stream-car s))
                (d (stream-cdr s)))
            (if (pred? a)
                (values d (list a) #f)
                (values d #f (list a))))))

  (lambda ()
    (stream-partition odd?
      (stream-range 1 6)))
  (lambda (odds evens)
    (list (stream->list odds)
          (stream->list evens))))
=> ((1 3 5) (2 4))

------- end of forwarded message -------

Posted on the users mailing list.