<div>
You might find this link useful as well <a href="http://en.wikipedia.org/wiki/Tail_call">http://en.wikipedia.org/wiki/Tail_call</a> - the reason that your new example is stacking is that having code after the recursion means that it's no longer tail-recursive, and will indeed consume stack space.</div>
<div><br><div>-- </div>Steve Strong<div>@srstrong</div><div><br>Sent with <a href="http://www.sparrowmailapp.com/?sig">Sparrow</a></div><br></div>
<p style="color: #A0A0A8;">On Friday, 16 September 2011 at 10:57, Jovi Zhang wrote:</p>
<blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px;">
<span><div><div>On Fri, Sep 16, 2011 at 4:44 PM, Robert Raschke <<a href="mailto:rtrlists@googlemail.com">rtrlists@googlemail.com</a>> wrote:<br><blockquote type="cite"><div><br>On Fri, Sep 16, 2011 at 9:38 AM, Jovi Zhang <<a href="mailto:bookjovi@gmail.com">bookjovi@gmail.com</a>> wrote:<br><blockquote type="cite"><div><br>Hi,<br> I am a newbie of Erlang, here I have a question for receive<br>message in Erlang loop.<br> Like below Erlang code, when process receive one message, it will<br>invoke loop again,<br> then it can service like a server, receive message forever.<br> BUT is possible that process will stack overflow after receive too<br>many message? the process invoke loop again and again.<br><br> I saw there have many code write like this in <<Programming Erlang>><br>book.<br><br>loop() -><br> receive<br> hello -><br> io:format("hello\n"),<br> loop();<br> Other -><br> io:format("I don't know what is this message, ~p is ~n"<br>,[Other]),<br> loop()<br> end.<br></div></blockquote><br>No stack overflow, due to tail call optimisation. See also<br><a href="http://www.erlang.org/doc/reference_manual/functions.html#id74170">http://www.erlang.org/doc/reference_manual/functions.html#id74170</a><br><br>Robby<br></div></blockquote><br>Thanks, but when I test it using escript, the result is like below,<br>"loop end" printed two times, it means it stacked!<br><br>[root@localhost Erlang]# cat jovi.erl<br>#!/usr/bin/env escript<br><br>loop() -><br> receive<br> one -><br> io:format("one\n"),<br> loop();<br> two -><br> io:format("two\n")<br> end,<br> io:format("loop end\n").<br><br><br>main(_) -><br> Pid = spawn(fun() -> loop() end),<br> Pid ! one,<br> Pid ! two,<br> receive<br> _Any -> void<br> end.<br><br><br>[root@localhost Erlang]# ./jovi.erl<br>one<br>two<br>loop end<br>loop end<br>^C<br>_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br><a href="http://erlang.org/mailman/listinfo/erlang-questions">http://erlang.org/mailman/listinfo/erlang-questions</a><br></div></div></span>
</blockquote>
<div>
<br>
</div>