list handling functions
Fri Sep 19 00:41:23 CEST 2003
On Thu, 18 Sep 2003, Vlad Dumitrescu wrote:
> Are there any ways to simply express a 'map' (or similar) function
> that iterates over several lists at once? I use something like
> map2(Fun, A, B) ->
> map2(Fun, A, B, ).
> map2(Fun, , _, Res) ->
> map2(Fun, _, , Res) ->
> map2(Fun, [Ha|Ta],[Hb|Tb],Res)->
> map2(Fun, Ta, Tb, [Fun(Ha, Hb)|Res]).
> but this seems to be general enough to be found in a library. Or maybe
> list comprehensions could be used in a clever way?
I think the "standard" (but less efficient) solution is to first use
a "zip" function on the two lists, creating a single list of 2-tuples
(in most list libraries you'll also find a zip3 function for zipping 3
lists, and in some cases there are zip4...zip9 as well).
Then you just do a normal "map" on the tuples. Your map2 can be
viewed as the result of a deforestation-style optimization of zip+map.
Richard Carlsson () (This space intentionally left blank.)
E-mail: WWW: http://user.it.uu.se/~richardc/
"Having users is like optimization: the wise course is to delay it."
-- Paul Graham
More information about the erlang-questions