[plt-scheme] Re: Stream comprehensions

From: David Van Horn (dvanhorn at cs.brandeis.edu)
Date: Mon Jul 2 17:09:37 EDT 2007

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.
> 
> 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?

You could also use SRFI 40 in combination with SRFI 42, Eager 
Comprehensions.  I don't recall if PLT's implementation of SRFI 42 
includes support for SRFI 40 streams by default (it should if it 
doesn't), but it is easy to do yourself using the code here:

    http://srfi.schemers.org/srfi-42/srfi40-ec.scm

If you take the opportunity to teach your son macros, this might be a 
good program to study (although it involves assignment and continuations).

David



Posted on the users mailing list.