[erlang-questions] Erlang again!

黃耀賢 (Yau-Hsien Huang) <>
Thu Jul 22 19:09:08 CEST 2010


Hi, boris.

On Fri, Jul 23, 2010 at 12:55 AM, Boris Mühmer <
> wrote:

> 2010/7/22 Masklinn <>:
> > What you want here is zip/2[0] or, even better, zipWith/3[1].
>


>    sub(L2,L1) ->
>        lists:reverse(sub(L2,L1,[])).
>
>    sub([],[],List) ->
>        List;
>    sub([H2|T2],[H1|T1],List) ->
>        sub(T2,T1,[(H2-H1)|List]).
>
> Ok, this is much longer than with zip or zipwith, but somehow this was
> much easier to "develop". Well, at the end of the documentation of
> zipwith there is almost a solution to the problem, but I wouldn't have
> found it, if zip or zipwith wasn't mentioned.
>
> Is such a solution plain bad, or just a newbie mistake/error?
>
>  - boris
>
>
It's not bad. It's a technique that is not related to what zip/2 or
zipWith/3
means, but it is a general way to make tail-recursion by using a
accumulating parameter. It's an important programming strategy for
a newbie to become familiar with Erlang or functional programming.


More information about the erlang-questions mailing list