[erlang-questions] re cursive fun()

Richard Carlsson <>
Sun Oct 5 11:30:22 CEST 2008

deepblue_other wrote:
> hello
> I have this going on
> FunVar = 
>    fun() ->
>       ...
>       FunVar()
>    end.
> so the compiler is complaining that FunVar is unbound at the place where its
> being used inside fun(); this makes sense, however Im wondering how to make
> this into a recursive function since there's no name to reference the
> function with. 

You have to make it keep "itself" around as an additional parameter,
so that you can first create it and then hand it the self-reference:

FunVar =
   fun(Myself) ->

then call it like this:


If you want the result to be a function of arity 0,
as in your example, then replace the last line with

FinalFunVar = fun () -> FunVar(FunVar) end


 "Having users is like optimization: the wise course is to delay it."
   -- Paul Graham

More information about the erlang-questions mailing list