Mapping over 2+ lists/variables?

View: New views
6 Messages — Rating Filter:   Alert me  

Mapping over 2+ lists/variables?

by cnb :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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>


Går det långsamt? Skaffa dig en snabbare bredbandsuppkoppling.
Sök och jämför priser hos Kelkoo.
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: Mapping over 2+ lists/variables?

by Richard Carlsson-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: Mapping over 2+ lists/variables?

by Lev Walkin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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>

Try opening a manual page for lists module
        erl -man lists
and look for zip, zipwith, zipwith3

> 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>


[X + Y || X <- lists:seq(1,10), Y <- lists:seq(1,10)].


--
vlm
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: Mapping over 2+ lists/variables?

by Sylvain Rousseau :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
You can do :
1> lists:zipwith(fun(X,Y) -> X+Y end, [1,2,3], [4,5,6]).
[5, 7, 9]

Does it help ?

Regards,
-- Sylvain

2008/7/23 Circular Function <circularfunc@...>:
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>


Går det långsamt? Skaffa dig en snabbare bredbandsuppkoppling.
Sök och jämför priser hos Kelkoo.
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: Mapping over 2+ lists/variables?

by Edwin Fine :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2> [X + Y || {X, Y} <- lists:zip([1,2,3], [4,5,6])].   
[5,7,9]


2008/7/23 Circular Function <circularfunc@...>:
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>


Går det långsamt? Skaffa dig en snabbare bredbandsuppkoppling.
Sök och jämför priser hos Kelkoo.
_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions



--
The great enemy of the truth is very often not the lie -- deliberate, contrived and dishonest, but the myth, persistent, persuasive, and unrealistic. Belief in myths allows the comfort of opinion without the discomfort of thought.
John F. Kennedy 35th president of US 1961-1963 (1917 - 1963)

_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions

Re: Mapping over 2+ lists/variables?

by Richard O'Keefe :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 24 Jul 2008, at 4:24 am, Circular Function wrote:
> 38> lists:map(fun(X,Y) -> X+Y end,[1,2],[3,4]).
> ** exception error: undefined function lists:map/3
> 39>


So write your own:

        map(F, [A|As], [B|Bs]) -> [F(A,B) | map(F, As, Bs)];
        map(_, [],     []    ) -> [].

I mean, it's less typing to FIX the problem than to complain
about it!

> isnt there general map-function that map is derived from that I can  
> use?

No.  The code for map is just

        map(F, [A|As]) -> [F(A) | map(F, As)];
        map(_, []    ) -> [].

Erlang is open source, and if you have it, you also have all the
source code, including <ERLHOME>/lib/stdlib/src/lists.erl,
so you can easily see for yourself how this and other functions
work.

>
> what about listcomprehensions?
> 44> [X+Y || X,Y <- lists:seq(1,10),lists:seq(1,10)].
> * 1: variable 'X' is unbound

Indeed it is.  Like Haskell (yah boo chiz) Erlang list
comprehensions do not permit "parallel" iteration, only
"nested" iteration.  This is one of the things I like
about Clean.  Hmm.  List comprehension uses '||', I've
just thought of a use for '&&'...

The Haskell approach would be

        [x + y | (x,y) <- zip [1..10] [1..10]]

or better still,
        zipWith (+) [1..10] [1..10]

The Erlang equivalent of using zip here is, well,
using zip:

        [X + Y | {X,Y} <- lists:zip(lists:seq(1,10),
                                    lists:seq(1,10))]

Personally I loathe this.  Some Haskell compilers
(notably GHC and I think YHC) are smart enough to do
'deforestation' and NOT really build a list of pairs.
The Erlang compiler is not.  (For one thing, 'zip'
is part of the Haskell 'standard prelude'; it really
counts as part of the language.  'zip' is not in the
erlang: module, and not even in any module in the
kernel application.  It's in the lists module, which
is in the stdlib application.  So the compiler is not
going to make any assumptions at all about what it does.)
I would prefer to see
        [X +  Y || X <- lists:seq(1,10)
                 && Y <- lists:seq(1,10)]

Sadly, that's not legal now.

--
If stupidity were a crime, who'd 'scape hanging?







_______________________________________________
erlang-questions mailing list
erlang-questions@...
http://www.erlang.org/mailman/listinfo/erlang-questions
LightInTheBox - Buy quality products at wholesale price!