- 1> Adder = fun(X) -> fun(Y) -> X + Y end end.
- #Fun<erl_eval.5.123085357>
- 2> Adder10 = Adder(10).
- #Fun<erl_eval.5.123085357>
- 3> Adder(10).
- 15
Line 1 defines an
Adder
that returns another function with X
bound to the input to Adder
. In line 2 we create a specific adder - in this case a function that adds 10 to its input. I think line 3 was meant to show how the specific adder created in line 2 could be invoked. If Adder10
were to be called with 5 as argument it would return 15. Instead Adder
is invoked again and the result shown is not correct as Adder(10)
would return a generator, not 15.
I think the snippet can be modified thus:
- 1> Adder = fun(X) -> fun(Y) -> X + Y end end.
- #Fun<erl_eval.6.49591080>
- 2> Adder10 = Adder(10).
- #Fun<erl_eval.6.49591080>
- 3> Adder10(5).
- 15