Erlang Language Anonymous function


Example

Here 2 examples of recursive anonymous functions based on previous example. Firstly, simple infinite loop:

InfiniteLoop = fun 
  R() -> 
    R() end.

Secondly, anonymous function doing loop over list:

LoopOverList = fun 
  R([]) -> ok;
  R([H|T]) ->
    R(T) end.

These two functions could be rewritten as:

InfiniteLoop = fun loop/0.

In this case, loop/0 is a reference to loop/0 from remarks. Secondly, with little more complex:

LoopOverLlist = fun loop/2.

Here, loop/2 is a reference to loop/2 from list example. These two notations are syntactic sugar.