[erlang-questions] list comprehension of two lists

Richard O'Keefe <>
Mon Jun 21 02:52:46 CEST 2010


On Jun 19, 2010, at 9:34 AM, Wes James wrote:

> I see how to do:
>
> [X || X <- [1,2,3,4,5,6]]
>
> will create [1,2,3,4,5]
>
> but how about I have two lists, [1,2,3] and [a,b,c] and I want to  
> output
>
> [[1,a], [2,b], [3,c]]
>
> can that be done with a list comprehension?

EEP 19 ( http://www.erlang.org/eeps/eep-0019.html )
would let you write

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

EEP 19 was inspired by a similar feature in Clean.
Until that or something like it is implemented, no,
it _can't_ be done with a list comprehension, at least
not in any way that's simpler than calling lists:zip2/2
or lists:zipwith/3.




More information about the erlang-questions mailing list