Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between head & tail recursion [duplicate]

I'm trying to get the difference between these 2 recursive strategies.

The definition I was told is the following:

Tail Recursion: A call is tail-recursive if nothing has to be done after the call returns i.e. when the call returns, the returned value is immediately returned from the calling function

Head Recursion: A call is head-recursive when the first statement of the function is the recursive call.

like image 770
Scarl Avatar asked Jan 29 '14 09:01

Scarl


People also ask

What is the difference between head and </ head?

"The <head> element represents a collection of metadata for the Document." "The <header> element represents a group of introductory or navigational aids." The main difference is that the <head> element is for META data and the <header> element is for actual content.

What is the difference between the head and header tags?

The head tag is used for holding Meta information, title, links, etc. and is not displayed on the page. The header tag is used within the body of the website and can be used multiple times if required, e.g. to determine the top of an article .

What is difference between head and title?

The title element is used to tell the browser what text to put in the tab for this page. The head tag is what belongs in the little tab on a webpage, so for example if you take google, they have that little G icon on the left of the tab, and that would be the head.

What is the difference between head and body in HTML?

A HTML file has headers and a "body" (payload) — just like a HTTP request. The <body> encapsulates the contents of the document, while the <head> part contains meta elements, i.e., information about the contents. This is (typically) title, encoding, author, styling etc.


1 Answers

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. Choosing between the two recursive styles may seem arbitrary, but the choice can make all the difference.

A function with a path with a single recursive call at the beginning of the path uses what is called head recursion. The factorial function of a previous exhibit uses head recursion. The first thing it does once it determines that recursion is needed is to call itself with the decremented parameter. A function with a single recursive call at the end of a path is using tail recursion. Refer this article

Example Recursion :

public void tail(int n)         |     public void head(int n)
{                               |     {
    if(n == 1)                  |         if(n == 0)
        return;                 |             return;
    else                        |         else
        System.out.println(n);  |             head(n-1);
                                |
    tail(n-1);                  |         System.out.println(n);
}                               |     }

If the recursive call occurs at the end of a method, it is called a tail recursion. The tail recursion is similar to a loop. The method executes all the statements before jumping into the next recursive call.

If the recursive call occurs at the beginning of a method, it is called a head recursion. The method saves the state before jumping into the next recursive call.

like image 68
Java Curious ღ Avatar answered Oct 17 '22 21:10

Java Curious ღ