Mutual recursion

This is an old revision of this page, as edited by TomCerul (talk | contribs) at 08:48, 24 June 2002 (re-installing bad style complaint in a hopefully more NPOV form. the WHY is important). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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

For instance, consider two functions A(x) and B(x) defined as follows:

 A(x) = 1         (when x≤1)
 A(x) = B(x + 2)  (when x>1)
 B(x) = A(x - 3) + 4

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.

Not all programming styles encourage mutual recursion. Some programmers avoid it because they find it confusing. Specifically it can be difficult to determine the circumstance which will provide non-recursive execution (an answer). Because of this difficulty, some programmers consider mutual recursion to be bad style.