Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tail Recursion vs Forward recursion in Erlang

Is tail recursion better than forward recursion for perfomance in erlang?
Or erlang compiler optimizes forward recursion too?
I mean, are there any reasons to use tail recursion instead of forward recursion?
In my opinion, forward recursion looks more pretty.

like image 851
Stan Avatar asked Feb 09 '11 08:02

Stan


People also ask

What is tail recursion in Erlang?

The function len(Rest) itself then needed the result of another function call to be found. The additions would get stacked until the last one is found, and only then would the final result be calculated. Tail recursion aims to eliminate this stacking of operation by reducing them as they happen.

What is the difference between recursion and tail recursion?

In head recursion , the recursive call, when it happens, comes before other processing in the function (think of it happening at the top, or head, of the function). In tail recursion , it's the opposite—the processing occurs before the recursive call.

What is difference between tail and non tailed recursion?

In tail recursion, there is no other operation to perform after executing the recursive function itself; the function can directly return the result of the recursive call. In non-tail recursion, some operations need to be performed using the returned value of the recursive call.

What is forward recursion?

Forward recursion involves moving in a direction from the first stage to the last stage. Backward recursion is the opposite, where the problem is solved from the last stage backward to the first stage. Forward recursion is advantageous for problems that involve uncertain time horizons (Kennedy, 1986).


1 Answers

Tail recursion and forward recursion are totally different concepts. See this discussion.

It is possible to write a forward recursion that is tail recursive, and thus optimized. It is also possible to write a forward recursion that is not tail recursive: in this case, it will not be optimized, i.e. it will consume stack space.

like image 184
tonio Avatar answered Oct 07 '22 17:10

tonio