In mathematics and computer science, mutual recursion is a form of recursion where two mathematical or computational functions are defined in terms of each other. It is sometimes called indirect recursion.
In mathematics, the Hofstadter Female and Male sequences are an example of a pair of integer sequences defined in a mutually recursive manner.
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. 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.Read more about Mutual Recursion: Example, Conversion To Direct Recursion
Famous quotes containing the word mutual:
“Every nation ... have their refinements and grossiertes.... There is a balance ... of good and bad every where; and nothing but the knowing it is so can emancipate one half of the world from the prepossessions which it holds against the otherthat [was] the advantage of travel ... it taught us mutual toleration; and mutual toleration ... taught us mutual love.”
—Laurence Sterne (17131768)