[plt-dev] I love Haskell (not an April Fools Joke!), feature request for 'for'

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Apr 1 11:22:55 EDT 2009

On Apr  1, Matthias Felleisen wrote:
> 
> > (define (fence s n)
> >   (define is
> >     (shared ((x (append (range 1 n) (range (- n 1) 2) x))) x))
> >   (define wv
> >     (for/list ((c s)) (begin0 (list c (car is)) (set! is (cdr is)))))
> >   (map first (sort2 wv)))
> 
> 
> These four lines demonstrate how a locally delimited use of mutation  
> achieves the same purpose.
> 
> FEATURE REQUEST: could for just treat something like is as a
> sequence? I have seen this trick I just used time and again over my
> Scheme career and we shouldn't have to expose the set! statement at
> all.

It could -- but you're using the generic form of the sequence, so it's
trying to guess what you want to iterate over.  This works:

  (define wv (for/list ([c s] [i (in-list is)]) (list c i)))


> (BTW, I don't think I can use the built-ins to turn this cyclic form
> of data into a 'sequence'.)

...but I think that the real need here is for more sequence
combinators.  In my Swindle iterator macros, I had this iteration
specification:

  (x <- something <- something-else)

which would iterate over the first thing and then over the second
one.  It works even with another sequence that is broken differently:

  => (list-of (list x y) (x <- 1 .. 4 <- 3 .. 1 and y <- '(x x) <- 1 ..))
  ((1 x) (2 x) (3 1) (4 2) (3 3) (2 4) (1 5))

Given that `for' uses a new kind of sequene values, it would be nice
to have some way to concatenate them

  (for/list ([x (seq+ (range 1 4) (range 3 1))]) x)

and if the parts of the `seq+' expressions are lazy, you'd be able to
do iterate with:

  (define waves
    (seq+ (range 1 n) (range (- n 1) 2) waves))
  (define wv
    (for/list ([c s] [i waves]) (list c i)))

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


Posted on the dev mailing list.