Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tail recursion vs. forward recursion

Tags:

Can someone give me the difference between these two kinds recursions and example (specifically in OCaml)?

like image 684
REALFREE Avatar asked Jun 15 '10 06:06

REALFREE


People also ask

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 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).

Why is tail recursion better?

The tail recursion is better than non-tail recursion. As there is no task left after the recursive call, it will be easier for the compiler to optimize the code. When one function is called, its address is stored inside the stack. So if it is tail recursion, then storing addresses into stack is not needed.

What are the three types of recursion?

Different types of the recursionDirect Recursion. Indirect Recursion. Tail Recursion.


1 Answers

A tail recursive function is a function where the only recursive call is the last one in the function. A non-tail recursive function is a function where that is not the case.

A backward recursion is a recursion where in each recursive call the value of the parameter is less than in the previous step. A forward recursion is a recursion where it grows bigger with each step.

Those are two orthogonal concepts, i.e. a forward recursion may or may not be tail-recursive and the same applies to backward recursions.

For example the factorial function is often written like this in imperative languages:

fac = 1 for i from 1 to n:     fac := fac * i 

The common recursive version of factorial counts backwards (i.e. it calls itself with n-1 as the parameter), however if you'd directly translate the above imperative solution, you'd come up with a recursive version that counts upwards. It would look something like this:

let fac n =   let rec loop i =     if i >= n     then i     else i * loop (i+1)   in     loop 1 

This is a forward recursion and as you can see it is slightly more cumbersome than the backward recursive variant as it requires a helper function. Now this is not tail recursive as the last call in loop is the multiplication, not the recursion. So to make it tail-recursive, you'd do something like this:

let fac n =   let rec loop acc i =     if i >= n     then acc     else loop (i*acc) (i+1)   in     loop 1 1 

Now this is both a forward recursion and a tail recursion because the recursive call is a) a tail-call and b) calls itself with a greater value (i+1).

like image 96
sepp2k Avatar answered Oct 12 '22 23:10

sepp2k