Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 
Share Thread:
Reddit Facebook Twitter
Tidy recursion
05-20-2013, 01:50 PM (This post was last modified: 05-20-2013 01:57 PM by dgud.)
Post: #4
RE: Tidy recursion
Micheus is right, the binary comprehension is the best for this code.

So example is kind of bad, for the question.
And the answer is that it depends, you will have to measure which one is fastest,
in current release, previously example 3 was always best and will be always be
if you don't need the lists:reverse at the end.
Now (since Björn have made some optimizations in erlang) if you really want
the fastest implementation you will have to measure, but for most of the
cases it doesn't matter if you use 1) or 3)
Number 2 is ugly and creates an unnecessary extra list.

If you want to return more than one thing only 3) is possible.

What I think Joe is saying is to avoid doing something like this.

init() ->
Result = loop(),
{ok, Result}.

loop() ->
receive
quit -> 42;
Msg -> io:format("Msg ~p~n",[Msg]),
loop()
end.

Which must save init function on the stack until loop returns.
See: http://en.wikipedia.org/wiki/Tail_call for a better explanation :-)
Reply


Messages In This Thread
Tidy recursion - nemyax - 05-17-2013, 12:22 PM
RE: Tidy recursion - micheus - 05-18-2013, 06:22 AM
RE: Tidy recursion - nemyax - 05-19-2013, 09:14 PM
RE: Tidy recursion - dgud - 05-20-2013 01:50 PM
RE: Tidy recursion - micheus - 05-20-2013, 01:58 PM

Forum Jump:


User(s) browsing this thread: 1 Guest(s)