[erlang-questions] Mapping over 2+ lists/variables?

Richard Carlsson richardc@REDACTED
Wed Jul 23 18:28:46 CEST 2008


Circular Function wrote:
> in python I can do:
>  >>> map(lambda x,y:x+y,[1,2,3],[4,5,6])
> [5, 7, 9]
>  >>>
> 
> 38> lists:map(fun(X,Y) -> X+Y end,[1,2],[3,4]).
> ** exception error: undefined function lists:map/3
> 39>
> 
> isnt there general map-function that map is derived from that I can use?
> what about listcomprehensions?
> 44> [X+Y || X,Y <- lists:seq(1,10),lists:seq(1,10)].
> * 1: variable 'X' is unbound
> 45>

Erlang's list comprehensions can't do that. The function you want is
lists:zipwith(Fun,List1,List2). You could also do lists:zip(List1,List2)
and then run map on the result, but zipwith does it in one pass.

     /Richard

-- 
  "Having users is like optimization: the wise course is to delay it."
    -- Paul Graham



More information about the erlang-questions mailing list