[erlang-questions] Erlang again!

黃耀賢 (Yau-Hsien Huang) <>
Thu Jul 22 18:43:28 CEST 2010

On Thu, Jul 22, 2010 at 7:28 PM, Jilani Khaldi <> wrote:

> sub(L2,L1) -> [X2-X1||X1<-L1,X2<-L2].
A comment for this line. It means that any element in L1 may be
subtracted by any elements in L2. Product of lists L1 and L2.

For your request, use Zip function (lists:zip/2 in Erlang) or write
a direct function:

    sub(Xs, Ys) when Xs = [] or Ys = 0 -> [];
    sub([X|Xs], [Y|Ys]) -> [X-Y|sub(Xs,Ys)].

By Map-After-Zip,

    sub(Xs, Ys) -> array.map(fun (X, Y) -> X-Y end, lists.zip(Xs, Ys)).

By lists.zipWith/3,

    sub(Xs, Ys) -> lists.zipWith(fun (X, Y) -> X-Y end, Xs, Ys).

More information about the erlang-questions mailing list