Mutual recursion

From Wikipedia, the free encyclopedia

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)=\begin{cases} 1 & , x\le1 \\ B(x+2) & ,x>1\end{cases}

B(x)=\begin{cases}A(x-3)+4\end{cases}


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.


In other languages