<div dir="ltr">Just noticed a mistake.<br><br>FunVar =<br>   fun(MySelf) -> <br>      ...<br>      MySelf().<br><br>should have been<br><br>FunVar =<br>   fun(MySelf) -> <br>      ...<br>      MySelf(MySelf).<br><br>
<div class="gmail_quote">On Sun, Oct 5, 2008 at 5:13 AM, Edwin Fine <span dir="ltr"><<a href="mailto:erlang-questions_efine@usa.net">erlang-questions_efine@usa.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div dir="ltr">I'm assuming that you want to do this in the shell, otherwise it would be easier just to write the function in the usual way, e.g.<br><br>myfun() -><br>    blah, blah,<br>    myfun().<br><br>To create a fun that calls itself, you must realize that you are creating an unnamed function. When you write<br>

<br>fun(X) -> X * 2 end.<br><br>this is compiled to a Fun data type, e.g. #Fun<erl_eval.6.13229925>.<br><br>Since the function does not have a name, it cannot refer to itself.<br><br>When you write FunVar = fun(X) -> X * 2 end, the function is bound to the variable FunVar. Because the binding takes place *after* the fun is created, the fun cannot refer to the variable FunVar within its definition.<br>

<br>So how is it done? You have to pass the variable to the function so that it "knows" what to call:<br><br>FunVar =<br>   fun(MySelf) -> <br>      ...<br>      MySelf().<br><br>Then when you call it, you call FunVar(FunVar). Looks strange but it works.<br>

<br>Example: Writing a simple generate and fold function using only a fun() variable.<br><br><span style="font-family: courier new,monospace;">9> GenFold = </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">   fun(0, Acc, _) -><br>

      Acc;</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">   (X, Acc, Fun) -><br>      Fun(X - 1, [X|Acc], Fun)</span><br style="font-family: courier new,monospace;">

<span style="font-family: courier new,monospace;">   end.</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#Fun<erl_eval.18.105910772></span><br style="font-family: courier new,monospace;">

<span style="font-family: courier new,monospace;">10> GenFold(10, [], GenFold).                                                      </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">[1,2,3,4,5,6,7,8,9,10]</span><br style="font-family: courier new,monospace;">

<span style="font-family: courier new,monospace;">11> </span><br><br>Or a for loop:<br><br><span style="font-family: courier new,monospace;">22> For = <br>    fun(Max, Max, ExecFun, _) -><br>       ok;<br>   (N, Max, ExecFun, Next) -><br>

      ExecFun(N),<br>      Next(N + 1, Max, ExecFun, Next)<br>   end.        </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">#Fun<erl_eval.4.105156089></span><br style="font-family: courier new,monospace;">

<span style="font-family: courier new,monospace;">23> For(0,10,fun(I) -> io:format("~B ", [I]) end, For).                                                                             </span><br style="font-family: courier new,monospace;">

<span style="font-family: courier new,monospace;">0 1 2 3 4 5 6 7 8 9 ok</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">24> </span><br><br>Stuff like this is described in Joe Armstrong's Erlang book.<br>

<br>Hope this helps.<div><div></div><div class="Wj3C7c"><br><br><div class="gmail_quote">On Sat, Oct 4, 2008 at 11:19 PM, deepblue_other <span dir="ltr"><<a href="mailto:cktgatb@gmail.com" target="_blank">cktgatb@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
hello<br>
I have this going on<br>
<br>
FunVar =<br>
   fun() -><br>
      ...<br>
      FunVar()<br>
   end.<br>
<br>
so the compiler is complaining that FunVar is unbound at the place where its<br>
being used inside fun(); this makes sense, however Im wondering how to make<br>
this into a recursive function since there's no name to reference the<br>
function with.<br>
<br>
thanks<br>
<font color="#888888">--<br>
View this message in context: <a href="http://www.nabble.com/recursive-fun%28%29-tp19820386p19820386.html" target="_blank">http://www.nabble.com/recursive-fun%28%29-tp19820386p19820386.html</a><br>
Sent from the Erlang Questions mailing list archive at Nabble.com.<br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
<br>
</font></blockquote></div><br></div></div></div>
</blockquote></div><br></div>