Headline

A form of single recursion suited for simple recursion removal

Illustration

A tail recursive function f is of the following form:

f(x) =

  • g(x), if p(x)
  • f(r(x)), otherwise
for suitable g, p, and r.

The schema variables g, p, and r serve these roles:

  • p is the condition of the base case.
  • g is the computation of the base case.
  • r is the computation for the argument of a recursive application.
We use Language:Java for illustration.

Consider the following tail-recursive formulation of a function for testing ints to be even.

	// Assume n>=0
	public static boolean even(int n) {
		if (n==0) return true;
		else if (n==1) return false;
		else return even(n-2);
	}

The function is tail-recursive, because the function returns right away after the recursive call.

For comparison, the following formulation is not tail-recursive; rather primitive recursion is used.

	// Assume n>=0
	public static boolean even(int n) {
		if (n==0) return true;
		else return !even(n-1);
	}

Every tail-recursive formulation can be easily translated into an iterative one, subject to tail-call elimination.

Metadata


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.