<br><br><div><span class="gmail_quote">16 Apr 2008 11:27:48 +0200, Bjorn Gustavsson <<a href="mailto:bjorn@erix.ericsson.se">bjorn@erix.ericsson.se</a>>:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
"É¢Î" <<a href="mailto:litaocheng@gmail.com">litaocheng@gmail.com</a>> writes:<br> <br> > Hi everyone.<br> > Recently I read <a href="http://www.erlang.org/download/armstrong_thesis_2003.pdf">http://www.erlang.org/download/armstrong_thesis_2003.pdf</a>.<br>
> In this paper 3.3.8, the author talk about tail-recursive:<br> > "Firstly the non tail-recursive way:<br> > factorial(0) -> 1;<br> > factorial(N) -> N * factorial(N-1).<br> > "<br> > I see this section is non tail-recursive. In my mind, the tail-recursive is<br>
> the better, because it do not consuming stack!<br> <br> <br>Yes, it is.<br> <br><br> > When I read OTP src, I find in the stdlib lists.erl. the map/2 write in this<br> > style:<br> > "map(F, [H|T]) -><br>
> [F(H)|map(F, T)];<br> > map(F, []) when is_function(F, 1) -> [].<br> <br> <br>Correct! It is not tail-recursive.<br> <br> The tail-recursive version would not consume any stack, but it would have to<br> build a list in reverse order, which it would have to reverse by calling lists:reverse/1.<br>
The stack space and the list consumes exactly the same amount of memory (in R12B).<br> <br> See<br> <br> <a href="http://www.erlang.org/doc/efficiency_guide/myths.html#2.3">http://www.erlang.org/doc/efficiency_guide/myths.html#2.3</a><br>
<br> /Bjorn<br> <br>--<br> Björn Gustavsson, Erlang/OTP, Ericsson AB<br> _______________________________________________<br> erlang-questions mailing list<br> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br> </blockquote></div><br>Thank you very much! Bjorn! :)