[erlang-questions] What is the purpose of the fun vs function distinction?

David Cabana drcabana@REDACTED
Sun Jan 13 21:22:50 CET 2008

I am trying to understand the purpose of the distinction between funs and
functions.  For instance, in the module foo let's define
    double(X) -> 2 * X.
Though double is perfectly nice function, the very natural
    lists:map(double, [1,2,3])
is not valid Erlang, not even inside foo. Instead one has to write
    lists:map(fun foo:double/1, [1,2,3]

Finding this tedious, I try another seemingly natural idea: I'll define my
functions as funs.
    doub = fun(X) -> 2*X end.

At first glance this seems to solve my problem, since I can write
    lists:map(doub, [1,2,3])
and things work as expected. But then I discover that I cannot export doub
from foo: it is a fun, not a function.

Well, things are what they are. Erlang simply does not have the clean syntax
of scheme or Haskell with respect to passing functions as arguments. But
why? Is this merely cruft, or does it serve some purpose?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080113/ef0b300f/attachment.htm>

More information about the erlang-questions mailing list