[erlang-questions] Erlang again!

Jesper Louis Andersen <>
Thu Jul 22 17:36:25 CEST 2010


On Thu, Jul 22, 2010 at 1:28 PM, Jilani Khaldi <> wrote:
> I have tried:
>
> sub(L2,L1) -> [X2-X1||X1<-L1,X2<-L2].
>

You are asking for the cartesian product of the values, example:

1> [{X1, X2} || X1 <- [1,2,3], X2 <- [4,5,6]].
[{1,4},{1,5},{1,6},{2,4},{2,5},{2,6},{3,4},{3,5},{3,6}

What you really are after is the zip of the two lists:

2> [{X1, X2} || {X1, X2} <- lists:zip([1,2,3], [4,5,6])].
[{1,4},{2,5},{3,6}]

As Masklinn hints, the functions lists:zipwith/3 and lists:zip/2 are
your friends in this case.


-- 
J.


More information about the erlang-questions mailing list