[plt-scheme] Stream comprehensions

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Jul 2 20:25:09 EDT 2007

On Jul  2, Mark Engelberg wrote:
> So a few months ago, my son started working on the Project Euler
> problems in Scheme.  After solving a few of the problems, he
> particularly enjoyed going through the forums, asking me to explain to
> him the solutions in the other programming languages.  He was struck
> by the brevity of the Haskell solutions, and begged me to teach him
> some Haskell.
> So for the past couple of months, he's been working the problems in Haskell.
> Now, I'm trying to move him back to Scheme to continue Htdp; problem
> is, he's been spoiled by some of the niceties of Haskell.  In
> particular, the ease of working with large or possibly infinite lists
> lazily, and using comprehensions to express complex cartesian
> products, maps and filters elegantly.
> Swindle's comprehension syntax is extremely close to Haskell's, so
> this is a pretty easy switch.

(FWIW, I think that the result-expression-first doesn't work well in
Scheme, where lexical scope usually goes to the right.  It's probably
more natural in a language that has `where' expressions...)

> For lazy lists, there are a few possibilities.
> 1.  Use the streams in srfi40.
> 2.  Show him how to implement streams himself using force/delay.
> 3.  Switch to Lazy Scheme language level and use regular lists.
> The problem is that none of these options seem to interact well with
> comprehensions.
> So... does anyone know of a comprehension macro system for DrScheme
> that interacts seamlessly with streams?  Should I roll my own using
> the collect macro in swindle, or has this already been done?

Using the collect macro in Swindle would be very easy -- there's even
a quick example in the docs.  But if all you need is a single
map+filter than a macro with lazy Scheme should make Haskell people
more at home.

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

Posted on the users mailing list.