[plt-scheme] Collect macro

From: Eli Barzilay (eli at barzilay.org)
Date: Fri Feb 21 03:39:51 EST 2003

FYI,

I have added a super `collect' macro to Swindle, which can be used to
implement list comprehension (done with `list-of'), and other related
functionality (`sum-of', `product-of', and `count-of').  It came out
flexible enough that I added a `loop-for' macro too, and I think it
covers almost everything you can do with CL's `loop' (but with a
better syntax and better philosophy, at least IMO).

It can iterate in parallel through different iterations which can
themselves be made of sequenced iterations, it can iterate with a
function by turning it into a co-routine, it has easy range iterations
and simple predefined iterations over all obvious Scheme types, it
does everything pretty efficiently (for example, not turning
everything to a list...), and it is easy to extend it.

The parallel looping is especially nice since an infinite iteration
can be used with a finite one, resulting in something equivalent to
using streams -- and with an issue similar to the odd/even thing.  So
with these macros you can do these things:

  (list-of x (c <- 1 .. 3 and x <- read-line))
or
  (list-of x (c <- 1 .. 3 and
              x <- (lambda (yield) (while #t (yield (read-line))))))

to return a list of three lines from in input, and this:

  (list-of (/ x) (c <- 1 .. 4 and x <- 4 3 ..))

returns the expected (1/4 1/3 1/2 1) without a division by zero error.

Finally, it can even collect things backward using an accumulator, or
forward with a simple recursion so it can be used to collect streams.

The full description is at

  http://www.cs.cornell.edu/eli/Swindle/misc-doc.html#collect

Any feedback is welcome, especially syntactic suggestions (I tried to
get something better than the CL `loop', but similar infix identifiers
seem to make the most convenient syntax).

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.