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

Alex Arnon <>
Tue Aug 8 11:37:00 CEST 2006


Well, in my mind the list comprehension goes through the statements
performing a pattern match, performing an "and". This includes the
extraction op ("<-"). So that would make sense in that context.
The map() function takes a fun, where you'd of course get a function_clause
mismatch.


On 8/8/06, Bengt Kleberg <> wrote:
>
> 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."
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20060808/94a65a9f/attachment.html>


More information about the erlang-questions mailing list