[erlang-questions] anyway to make recursion inside anonymous funs?

Daniel Luna luna@REDACTED
Wed Mar 28 18:37:29 CEST 2007

On Thu, 29 Mar 2007, June Kim wrote:
> "fun" expressions can be used to declare a function. Can it make a
> recursive call inside?
> That is,
> Can I somehow declare the following with callself, which is an
> imaginary functionality that calls the anonymous function itself?
> fun(X) -> receive {P,V} -> P!(X+V), callself(X+V) end end.

Fun = fun(F, X) -> receive {P,V} -> P!(X+V), F(F, X+V) end end,
Fun(Fun, X0),

So you just need to define the function that you want to call and then add 
the function as an argument to itself.

The joys of dynamic typing (or infinite types).

Just a warning though. Self recursive anonymous functions don't handle hot 
code updates.

Daniel Luna                           | Top reasons that I have a beard:
luna@REDACTED                     |  a) Laziness.
http://www.update.uu.se/~luna/        |  b) I can.
Don't look at my homepage (it stinks).|  c) I can get away with it.

More information about the erlang-questions mailing list