41

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.

1

5 Answers 5

55

Since OTP 17.0 there are named funs:

1> Perms = fun F([]) -> [[]]; F(L) -> [[H|T] || H <- L, T <- F(L--[H])] end.    
#Fun<erl_eval.30.54118792>
2> Perms([a,b,c]).
[[a,b,c],[a,c,b],[b,a,c],[b,c,a],[c,a,b],[c,b,a]]

Before that you could do this with a little argument trick:

1> Foo = fun(F, X) -> F(F, X) end.
#Fun<erl_eval.12.113037538>
2> Foo(Foo, a).
<...infinite loop!>

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:

1> Foo = fun(X) -> Fun = fun(F,Y) -> F(F,Y) end, Fun(Fun,X) end.
#Fun<erl_eval.6.13229925>
2> Foo(a).

For example:

1> Foo = fun(Max) ->
1>     Fun = fun(F, X) when X > Max -> [];
1>              (F, X) -> [X | F(F, X+1)]
1>           end,
1>     Fun(Fun, 0)
1> end.
#Fun<erl_eval.6.13229925>
2> Foo(10).
[0,1,2,3,4,5,6,7,8,9,10]
4
  • 2
    I have no experience with Erlang, but it would seem to me that the two arguments a and Foo should be reversed... Commented May 15, 2009 at 8:43
  • 1
    Does this answer portend the Y-combinator, as mentioned in another answer here? Commented May 17, 2009 at 5:47
  • Well, you could see it as a self-contained ("poor man's") Y-combinator perhaps... Commented Sep 3, 2009 at 19:14
  • 1
    @AdamLindberg: I like to call it half Y-combinator. Commented Aug 29, 2015 at 19:23
22

After Erlang 17, you can also use the "Funs with names" variant:

Foo = fun F(X) -> F(X) end.

In this way it is easier to understand that F is the function itself within the definition. Also, Foo and F can be the same variable.

1
  • Super! It is worked! I like this functionality very much! This solution is simple and convenient. Commented Jul 1, 2021 at 7:24
17

Alternatively, you can use the Y combinator. Y Combinator in Erlang explains.

4
  • Learning about the Y-combinator has been on my list of things to learn for a long time. Thanks for pointing out the opportune moment :). For people who know Scheme, I thought the following page was useful: dangermouse.brynmawr.edu/cs245/ycomb_jim.html Commented May 17, 2009 at 5:46
  • 1
    Tim, I think this answer could have been better had you briefly mentioned what the Y-combinator is about. I probably would have accepted this answer had you done that. Commented May 17, 2009 at 5:50
  • The link in the answer didn't work for me, but I found part 1 and part 2 of this walk-through on implementing the y combinator in erlang extremely helpful. I'm still wrapping my head around it, but it's a nice step-by-step just like the one for Scheme above.
    – laindir
    Commented Jun 20, 2014 at 19:17
  • 2
    The link is now broken. Commented May 9, 2017 at 11:11
1

I had a need to quickly send some packets over UDP for testing and here is how I have done it using the samples above:

Sendtimes = fun F(0,Socket) -> ok;
        F(Times,Socket) -> gen_udp:send(Socket, {127,0,0,1}, 5555, ["Message #:" ++ [Times]]),
        F(Times-1,Socket) end.
-2

Obviously, Foo gets assigned only after the fun is defined, so it may not be accessed from within it.

I don't think that Erlang allows to call the anonymous function from itself. Just make it a named one.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.