[erlang-questions] [erlang-question] How to comprehend two lists synchronously?

Barco You barcojie@REDACTED
Fri Nov 18 07:38:31 CET 2011


Hi Dmitry,

What your suggested can really solve my problem, but it's not
Tail-Recursion. The tail-recursed solution should look like this;

map2(_Fun, [], []) ->
   [];
map2(Fun, L1, L2) ->
   map2(Fun, L1, L2, []).

map2(_Fun, [], [], L) ->
   lists:reverse(L);
map2(Fun, [H1 | T1], [H2 | T2], L) ->
   map2(Fun, T1, T2, [Fun(H1, H2) | L]).


However, I'm still disappointed with the list comprehension which is
different from what I intuitively imagine about it.


Regards,
Barco

On Fri, Nov 18, 2011 at 1:49 PM, Dmitry Demeshchuk <demeshchuk@REDACTED>wrote:

> My guess is you have to zip them together, or just write a
> tail-recursed function:
>
> map2(Fun, [H1 | T1], [H2 | T2]) ->
>    [Fun(H1, H2) | map2(Fun, T1, T2)];
> map2(Fun, [], []) ->
>    [].
>
> The second option definitely isn't a list comprehension, but it
> requires less memory and has lesser complexity.
>
> On Fri, Nov 18, 2011 at 9:45 AM, Barco You <barcojie@REDACTED> wrote:
> > Dear Erlangers,
> >
> > I hope to get a list from two lists like this:
> > [{a1,b1}, {a2,b2}, {a3,b3}]      <-     [a1, a2 a3],  [b1, b2, b3].
> > But if I use list comprehension, I got:
> > 10>  [{D1,D2} ||  D1 <- [a1,a2,a3], D2 <- [b1,b2,b3]].
> > [{a1,b1},
> >  {a1,b2},
> >  {a1,b3},
> >  {a2,b1},
> >  {a2,b2},
> >  {a2,b3},
> >  {a3,b1},
> >  {a3,b2},
> >  {a3,b3}]
> >
> > So, my questions is how to comprehend list in synchronous way in order to
> > get what I want, rather than to compose the elements from two lists in
> all
> > possible situations.
> >
> > Thank you,
> > Barco
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
> >
>
>
>
> --
> Best regards,
> Dmitry Demeshchuk
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111118/49350a40/attachment.htm>


More information about the erlang-questions mailing list