List comprehensions

Rijk J. C. van Haaften rjchaaft@cs.uu.nl
Thu, 30 Jan 2003 12:06:06 +0100


>* Rijk J. C. van Haaften <rjchaaft@cs.uu.nl> [2003-01-30 11:41 +0100]:
>
> > Recently, I came accross this expression:
> > [ x + y | x <- xs | y <- ys ]
>                     ^
>                     Put a comma ',' here.

That's something totally different. Two examples:
1. Comma
   [ x + y | x <- [1,2], y <- [3,4] ]
= [4,5,5,6]

2. Bar
   [ x + y | x <- [1,2] | y <- [3,4] ]
= [ x + y | (x,y) <- zip [1,2] [3,4] ]
= zipWith (+) [1,2] [3,4]
= [4,6]

The first is according to the standard. No problems so far.
However, I couldn't find a description of the semantics of
the second (and it is clearly non-standard), though I think
the semantics given above using zip and zipWith are correct.

Rijk