lists:zip / zip_with / unzip etc

Thomas Arts thomas@REDACTED
Fri Jul 5 08:34:23 CEST 2002


Hi Heinz

The zip function is indeed missing in the lists module,
like the uniq function (removing duplicates from a list).

> ziph([H1|L1],[H2|L2], Z) ->
>     ziph(L1,L2,[{H1,H2}|Z]);
> ziph([],L2, Z) ->
>     Z;
> ziph(L1,[], Z) ->
>     Z.
> 
> zip(A,B) ->
>     lists:reverse(ziph(A,B,[])).

This solution for zip has complexity 2N (2 times the length
of the list). It is easy to make a solution that only
traverses the list once.
It is a matter of taste whether you accept lists of
unequal length. I would like the program to crash (in the
Erlang style) if you offer it two lists of unequal length.

/Thomas



More information about the erlang-questions mailing list