I’m trying to write a recursive fun in an Erlang shell, but I keep getting an unbound variable exception:
1> Foo = fun(X) -> Foo(X) end.
* 1: variable 'Foo' is unbound
This probably goes without saying, but I’m not trying to create an infinite loop! This is just a simple example of the error I’m getting.
Since OTP 17.0 there are named funs:
Before that you could do this with a little argument trick:
The trick here is to send in the function as an argument to itself to allow recursion.
Alternative way to make it in one shoot:
For example: