[erlang-questions] passing a function to lists:map
Mon Mar 22 13:34:44 CET 2010
Martin DeMello wrote:
> ta(X) when is_list(X) -> list_to_atom(X);
> ta(X) when is_tuple(X) -> tuple_to_list(X);
> ta(X) when is_integer(X) -> X.
> % this doesn't work
> atomize(List) -> lists:map(ta, List).
> % this does
> atomize(List) -> lists:map(fun(X) -> ta(X) end, List).
> What's the difference? Have I just got the syntax wrong, or is there
> some fundamental reason I need the second version?
There is a very fundamental reason. In Erlang 'ta' is just an atom; it
does not have a function type as it does in e.g. ML or Haskell.
Functions in Erlang do not only have a name but also an arity (*) hence
the need to use the "fun ta/1" construct there, as many have mentioned.
(*) for example in Erlang it is possible to have two functions with the
same name but different arities.
More information about the erlang-questions