an unexpected (to me) difference between lists:map/2 and list comprehension

Bengt Kleberg <>
Tue Aug 8 08:03:34 CEST 2006


greetings,

there is a difference between lists:map/2 and list comprehension that 
can hide errors in the code. the generator part works like a filter, 
silently dropping unmatched items in the list. it would be nice if this 
was mentioned in the documentation.


from the documentation and examples of list comprehension 
(http://www.erlang.se/doc/doc-5.5/doc/programming_examples/part_frame.html) 
i have assumed that the generator part works like the fun head in 
lists:map/2 (or lists:filter/2, etc).
ie the following two pices of code would be equivalent:

1) [A || {A, B, C} <- L].
2) lists:map( fun({A, B, C}) -> A end, L).

however, using the list L = [{a,b,c}, {1,2,3,4}].
i get the following:
1) [a]
2) ** exited: 
{function_clause,[{erl_eval,'-inside-a-shell-fun-',{{1,2,3,4}}},
                              {erl_eval,expr,3}]} **


bengt
-- 
    EPO guidelines 1978: "If the contribution to the known art resides
    solely in a computer program then the subject matter is not
    patentable in whatever manner it may be presented in the claims."



More information about the erlang-questions mailing list