<br><br><div><span class="gmail_quote">2007/5/31, Jason Dusek <<a href="mailto:jsnx@hellokitty.com">jsnx@hellokitty.com</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>Its tail recursive but, unfortunately, a little redundant -- I'd like<br>to rewrite it like this:<br><br>server({X, Y}) -><br> receive<br> {update, NewState} -><br> server(NewState);<br> {x, Client} ->
<br> Client ! X;<br> {y, Client} -><br> Client ! Y;<br> end,<br> server({X, Y})<br> .<br><br>But I worry that the compiler and interpreter will fail to recognize<br>my server as tail recursive. Will my rewrite blow the stack?
<br></blockquote></div><br>Yup, since the call to server/1 from within the receive clause is not in fact a tail call.<br><br>A common way to solve this dilemma is to do this:<br><br>server({X, Y} = State) -><br> S1 =
<br> receive<br> {update, NewState} -><br> NewState;<br> {x, Client} -><br> Client ! X,<br> State;<br> {y, Client} -><br> Client ! Y,
<br> S<br> end,<br> server(S1).<br><br>I added the "= State" in the function head so I wouldn't have to rebuild the tuple when nothing has changed.<br><br>BR,<br>Ulf W<br>