[erlang-questions] re cursive fun()

Richard O'Keefe ok@REDACTED
Mon Oct 6 04:30:06 CEST 2008


On 5 Oct 2008, at 4:19 pm, deepblue_other wrote:
> I have this going on
>
> FunVar =
>   fun() ->
>      ...
>      FunVar()
>   end.
>
Shouldn't this go in a FAQ somewhere?
Maybe it belongs in the reference manual.

The quick answer is that if you need a recursive
function, why not write a recursive function in the
usual way?

There's something particularly nasty about this
example.  One normally proves (or at any rate
argues with vigourously waving hands for) the
termination of a recursive function by showing
that some non-negative measure of its arguments
strictly decreases on each recursive call, but
this function has no arguments.  I think we need
to see more of it.

The long answer is that you can always do

	Rec = fun (Self, Args) ->
		....
		Self(Self, Args')
	      end,
	Foo = fun (Args) -> Rec(Rec, Args) end,

It's possible, but you are working against the
grain of the language, and there is most likely
a better way to achieve your goals.




More information about the erlang-questions mailing list