List comprehensions (was: Re: Binary comprehensions)

Carsten Schultz <>
Fri Aug 13 16:43:59 CEST 2004

Hi Luke!

On Fri, Aug 13, 2004 at 03:44:24PM +0200, Luke Gorrie wrote:
> Speaking of comprehensions, does anyone else think that the way
> multiple generators in a list comprehension works is weird? I think
> this behaviour would be better:
>   [{X,Y} || X <- [1,2,3], Y <- [a,b,c]]    => [{1,a},{2,b},{3,c}]
> i.e. to walk over the lists in parallel instead of trying all the
> combinations. I'm always writing code that walks down two lists in
> parallel and very rarely wanting all the combinations.

No, I do not feel like that at all.

BTW, I do not find a function like

zip([], _) -> [];
zip(_, []) -> [];
zip([X|Xs], [Y|Ys]) ->
    [{X, Y}|zip(XS, YS)].

Have I overlooked it?

GHC extends Haskell's syntax in a way you might like:

An adaption to Erlang would look like

[{X,Y} || X <- [1,2,3] || Y <- [a,b,c]]    => [{1,a},{2,b},{3,c}]

> I'm not suggesting it be changed (of course). I just think it's funny
> that list comprehensions turn up in lots of programminig languages and
> I hardly ever see someone use more than one generator at a time.

Hm, I am not sure here.  Several generators might indeed be needed
rarely because

[{X,Y} || X <- [1,2,3], Y <- [a,b,c]]

can be replaced by

lists:append([[{X,Y} || X <- [1,2,3]] || Y <- [a,b,c]])

and you might even omit the call to append in many cases in a usual
Erlang program.



Carsten Schultz (2:38, 33:47), FB Mathematik, FU Berlin
PGP/GPG key on the key servers, 
fingerprint on my home page.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <>

More information about the erlang-questions mailing list