Step 1: Define a function
add_one
in a module named test
.
- % test.erl
- -module(test).
- -export([add_one/1]).
- add_one(X) -> X + 1.
Step 2: Start the erlang shell and try to use this function.
- 1> c(test).
- {ok,test}
- 2> test:add_one(2).
- 3
- 3> lists:map(test:add_one, [1, 2, 3]).
- * 1: illegal expression
- 4> lists:map(fun(X) -> test:add_one(X) end, [1, 2, 3]).
- [2,3,4]
- 5> lists:map(fun(X) -> X + 1 end, [1, 2, 3]).
- [2,3,4]
- 6>
As you can see I have tried three different combinations. Line #3 shows a call to the higher order function
lists:map
passing in the qualified name of the add_one
function as first argument. The shell throws an error.
In Line #4 I wrap
test:add_one
inside a fun
and it works.
In Line #5 I replicate the code of
add_one
inside an anonymous fun
and everything works fine.
Contrast this with say, Python.
Definition:
- def add_one(x): return x + 1
Usage:
- >>> from test import add_one
- >>> map(add_one, [1, 2, 3])
- [2, 3, 4]
- >>>
Now I'll repeat the question: Does Erlang allow you to pass functions (not "funs" or anonymous functions) as arguments to higher order functions? Or am I missing something?
Please share your thoughts in the comments.
Update #1:
Got an answer. There is a way to do it. It still involves using the
fun
keyword.
- 1> c(test).
- {ok,test}
- 2> lists:map(fun test:add_one/1, [1, 2, 3]).
- [2,3,4]
- 3>
Update #2:
Got another answer which does *not* use
fun
. The syntax is just a little different. Thanks to Justin Sheehy.
- 1> c(test).
- {ok,test}
- 2> lists:map({test,add_one}, [1, 2, 3]).
- [2,3,4]
- 3>