<div dir="ltr">But, wait! Infinite (well, unbounded) recursion is a bad thing, right? At the very least it will blow up the stack, right?<br><div><br></div><div>Well, yes. It would. Except for <i>tail recursion</i>. When the recursive step is at the end of the function (and there's no lingering state to maintain), the compiler can optimize the recursion into a simple loop. As a result you get the cleanliness of recursive solution without having to worry about the space needed by the calculation being unbounded. <br><br>Cheers,</div><div>--ag</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Jun 17, 2019 at 11:06 AM I Gusti Ngurah Oka Prinarjaya <<a href="mailto:okaprinarjaya@gmail.com">okaprinarjaya@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>OMG. Finally..... I know and understand how this code flowing. It takes days for me to understand. </div><div>It turns out this code below:<br><br></div><div><span style="font-family:"courier new",monospace">{ok, AcceptorSocket} = gen_tcp:accept(ListenSocket),</span><br></div><div><br></div><div>is locking / blocking the execution of these lines:</div><div><br></div><div><span style="font-family:"courier new",monospace">1. spawn(fun() -> acceptor(ListenSocket) end),</span><br style="font-family:"courier new",monospace"><span style="font-family:"courier new",monospace">2. handle(AcceptorSocket).</span><br></div><div><br></div><div>Then, after we doing a connection to the port via telnet, the locking / blocking become released / unlocked / unblocked, then executing lines: 1 and  create new process,</div><div>lines: 2 listen / waiting the incoming message. Then make a new locking at the new created process for the next telnet connection. Yes this is an infinity recursive. </div><div>The base case is killing main process that started from <font face="courier new, monospace">start_server/1</font> . <font face="courier new, monospace">exit(Pid, killed)</font></div><div><br></div><div>Thank you :)</div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Pada tanggal Jum, 14 Jun 2019 pukul 11.45 I Gusti Ngurah Oka Prinarjaya <<a href="mailto:okaprinarjaya@gmail.com" target="_blank">okaprinarjaya@gmail.com</a>> menulis:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>I learn gen_tcp from learnyousomeerlangg book. I try to understand flow of code below. </div><div>And i got confused with acceptor/1 . Because acceptor/1 call itself but have no base case and just execute once. It's not usual. Why? <br>From my understanding it should be an infinity recursive.<br><div><br></div><div><font face="courier new, monospace">-module(naive_tcp).<br>-compile(export_all).<br><br>start_server(Port) -><br>  Pid = spawn_link(<br>    fun() -><br>      io:format("Spawned at start_server()~n"),<br>      {ok, ListenSocket} = gen_tcp:listen(Port, [binary, {active, false}]),<br>      spawn(fun() -> acceptor(ListenSocket) end),<br>      timer:sleep(infinity)<br>    end<br>  ),<br>  {ok, Pid}.<br><br>acceptor(ListenSocket) -><br>  io:format("I am acceptor~n"),<br>  {ok, AcceptorSocket} = gen_tcp:accept(ListenSocket),<br>  spawn(fun() -> acceptor(ListenSocket) end),<br>  handle(AcceptorSocket).<br><br>handle(AcceptorSocket) -><br>  inet:setopts(AcceptorSocket, [{active, once}]),<br>  receive<br>    {tcp, AcceptorSocket, <<"quit", _/binary>>} -><br>      gen_tcp:close(AcceptorSocket);<br>    {tcp, AcceptorSocket, Message} -><br>      gen_tcp:send(AcceptorSocket, Message),<br>      handle(AcceptorSocket)<br>  end.</font><br></div><div><br></div><div>Please enlightenment </div></div><div><br></div><div>Thank you </div><div><br></div><div><br></div></div>
</blockquote></div>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Artie Gold, Austin, Texas<br>--<br><a href="http://makeitsimpler.org" target="_blank">http://makeitsimpler.org</a></div></div></div></div></div>