Mutual recursion

Mutual recursion

Mutual recursion is a form of recursion where two mathematical or computational functions are defined in terms of each other.[1]

For instance, consider two functions even? and odd? defined as follows:

function even?(number : Integer)
    if number == 0 then
        return true
    else
        return odd?(abs(number)-1)
function odd?(number : Integer)
    if number == 0 then
        return false
    else
        return even?(abs(number)-1)

These functions are based on the realization that the question is three even is equivalent to the question, is two odd, which is the same as asking if 1 is even or 0 is odd. In the end, the answer is no, as realized by the function odd?. The abs function is used to ensure that number decrements towards zero even when it starts off as a negative value.

Mutual recursion is very common in the functional programming style, and is often used for programs written in LISP, Scheme, ML, and similar languages. In languages such as Prolog, mutual recursion is almost unavoidable.

Some programming styles discourage mutual recursion, claiming that it can be confusing to distinguish the conditions which will return an answer from the conditions that would allow the code to run forever without producing an answer. It is usually possible to turn two mutually recursive functions into a single recursive function by inlining the code of one into the other, possibly at the expense of legibility. Peter Norvig points to a design pattern which discourages the use entirely, stating

If you have two mutually-recursive functions that both alter the state of an object, try to move almost all the functionality into just one of the functions. Otherwise you will probably end up duplicating code.
—Peter Norvig [2]

Any mutual recursion can be converted to direct recursion using procedural inlining.[3]

In mathematics, the Hofstadter Female and Male sequences are an example of a pair of integer sequences defined in a mutually recursive manner.

See also

References

  1. ^ Manuel Rubio-Sánchez, Jaime Urquiza-Fuentes,Cristóbal Pareja-Flores (2002), 'A Gentle Introduction to Mutual Recursion', Proceedings of the 13th annual conference on Innovation and technology in computer science education, June 30–July 2, 2008, Madrid, Spain.
  2. ^ Solving Every Sudoku Puzzle
  3. ^ On the Conversion of Indirect to Direct Recursion by Owen Kaser, C. R. Ramakrishnan, and Shaunak Pawagi at State University of New York, Stony Brook (1993)

Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Recursion (computer science) — Recursion in computer science is a way of thinking about and solving problems. It is, in fact, one of the central ideas of computer science. [cite book last = Epp first = Susanna title = Discrete Mathematics with Applications year=1995… …   Wikipedia

  • Tail recursion — In computer science, tail recursion (or tail end recursion) is a special case of recursion in which the last operation of the function is a recursive call. Such recursions can be easily transformed to iterations. Replacing recursion with… …   Wikipedia

  • Extensible Embeddable Language — The Extensible Embeddable Language (EEL) is a scripting and programming language, focus on hard real time applications.EEL uses a C like syntax with higher level features not existing in the C programming language, which makes it safe in most of… …   Wikipedia

  • List of mathematics articles (M) — NOTOC M M estimator M group M matrix M separation M set M. C. Escher s legacy M. Riesz extension theorem M/M/1 model Maass wave form Mac Lane s planarity criterion Macaulay brackets Macbeath surface MacCormack method Macdonald polynomial Machin… …   Wikipedia

  • Pascal (programming language) — Pascal Paradigm(s) imperative, structured Appeared in 1970 Designed by Niklaus Wirth Typing discipline static, strong, safe …   Wikipedia

  • Data Protection Act — The Data Protection Act (DPA) is a United Kingdom Act of Parliament. It defines a legal basis for the handling in the UK of information relating to living people. It is the main piece of legislation that governs protection of personal data in the …   Wikipedia

  • HITS algorithm — Hyperlink Induced Topic Search (HITS) is a link analysis algorithm that rates Web pages, developed by Jon Kleinberg. It determines two values for a page: its authority, which estimates the value of the content of the page, and its hub value,… …   Wikipedia

  • Funktionsprototyp — Als Funktionsprototyp bezeichnet man in verschiedenen Programmiersprachen (vor allem C und C++) die Deklaration einer Funktion – inklusive Angaben über Anzahl und Typ der Parameter und Typ des Rückgabewertes – getrennt von ihrer Implementierung… …   Deutsch Wikipedia

  • Forcing (mathematics) — For the use of forcing in recursion theory, see Forcing (recursion theory). In the mathematical discipline of set theory, forcing is a technique invented by Paul Cohen for proving consistency and independence results. It was first used, in 1963,… …   Wikipedia

  • Function (mathematics) — f(x) redirects here. For the band, see f(x) (band). Graph of example function, In mathematics, a function associates one quantity, the a …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”