[Haskell-cafe] zip comprehensions and local decls

Christian Sievers sievers at math2.nat.tu-bs.de
Thu Mar 25 23:26:55 EST 2004


This is about a misconception I had about zip comprehensions and a possible
extension to get the desired result.  As zip compr. are implemented in hugs
and in ghc (though I only tried hugs), I thought that haskell-cafe might be a
good place for it.

In hugs mode, I expected something like the following to work:

  [ (a,a*x) | x <- [1..5] | y <- [10..], let a=x+y ]

but hugs says: ERROR - Undefined variable "x"

I think I can see why: 
The expression is rewritten to
  [ (a,a*x) | (x,y) <- zip [ x | x<-[1..5] ]
                           [ y | y <- [10..], let a=x+y ] ]

This scheme is useful if one has   ...|y<-list, let a=..., sometest a
which drops some elements from the list before zipping.

But it would be nice to have a way to drop already zipped tuples and to allow
such usages of local declarations.  Maybe a final '|' branch without
generator, so to get what was intended with the expression above one might

  [ (a,a*x) | x <- [1..5] | y <- [10..] | let a=x+y ]

or, as another example, one could have

  [ (x,y) | x <- [1..5] | y <- [10..] | even x ]

giving [(2,11),(4,13)].

So the idea is to rewrite
  [ e | oldstyle parallel list comprehension | declsandpreds ]
  [ e | zipN <as before...>                  , declsandpreds ]

Is this clear enough?
Does it sound useful?  Can you think of a nicer syntax?

All the best
Christian Sievers

More information about the Haskell-Cafe mailing list