<s>
In	O
mathematics	O
and	O
computer	B-General_Concept
science	I-General_Concept
,	O
mutual	B-Algorithm
recursion	I-Algorithm
is	O
a	O
form	O
of	O
recursion	O
where	O
two	O
mathematical	O
or	O
computational	O
objects	O
,	O
such	O
as	O
functions	O
or	O
datatypes	O
,	O
are	O
defined	O
in	O
terms	O
of	O
each	O
other	O
.	O
</s>
<s>
Mutual	B-Algorithm
recursion	I-Algorithm
is	O
very	O
common	O
in	O
functional	B-Language
programming	I-Language
and	O
in	O
some	O
problem	O
domains	O
,	O
such	O
as	O
recursive	B-Application
descent	I-Application
parsers	I-Application
,	O
where	O
the	O
datatypes	O
are	O
naturally	O
mutually	B-Algorithm
recursive	I-Algorithm
.	O
</s>
<s>
The	O
most	O
important	O
basic	O
example	O
of	O
a	O
datatype	O
that	O
can	O
be	O
defined	O
by	O
mutual	B-Algorithm
recursion	I-Algorithm
is	O
a	O
tree	B-Application
,	O
which	O
can	O
be	O
defined	O
mutually	O
recursively	O
in	O
terms	O
of	O
a	O
forest	O
(	O
a	O
list	O
of	O
trees	O
)	O
.	O
</s>
<s>
A	O
forest	O
f	O
consists	O
of	O
a	O
list	O
of	O
trees	O
,	O
while	O
a	O
tree	B-Application
t	O
consists	O
of	O
a	O
pair	O
of	O
a	O
value	O
v	O
and	O
a	O
forest	O
f	O
(	O
its	O
children	O
)	O
.	O
</s>
<s>
This	O
definition	O
is	O
elegant	O
and	O
easy	O
to	O
work	O
with	O
abstractly	O
(	O
such	O
as	O
when	O
proving	O
theorems	O
about	O
properties	O
of	O
trees	O
)	O
,	O
as	O
it	O
expresses	O
a	O
tree	B-Application
in	O
simple	O
terms	O
:	O
a	O
list	O
of	O
one	O
type	O
,	O
and	O
a	O
pair	O
of	O
two	O
types	O
.	O
</s>
<s>
This	O
mutually	B-Algorithm
recursive	I-Algorithm
definition	O
can	O
be	O
converted	O
to	O
a	O
singly	O
recursive	O
definition	O
by	O
inlining	O
the	O
definition	O
of	O
a	O
forest	O
:	O
</s>
<s>
A	O
tree	B-Application
t	O
consists	O
of	O
a	O
pair	O
of	O
a	O
value	O
v	O
and	O
a	O
list	O
of	O
trees	O
(	O
its	O
children	O
)	O
.	O
</s>
<s>
This	O
definition	O
is	O
more	O
compact	O
,	O
but	O
somewhat	O
messier	O
:	O
a	O
tree	B-Application
consists	O
of	O
a	O
pair	O
of	O
one	O
type	O
and	O
a	O
list	O
of	O
another	O
,	O
which	O
require	O
disentangling	O
to	O
prove	O
results	O
about	O
.	O
</s>
<s>
In	O
Standard	B-Language
ML	I-Language
,	O
the	O
tree	B-Application
and	O
forest	O
datatypes	O
can	O
be	O
mutually	O
recursively	O
defined	O
as	O
follows	O
,	O
allowing	O
empty	O
trees	O
:	O
</s>
<s>
Just	O
as	O
algorithms	O
on	O
recursive	O
datatypes	O
can	O
naturally	O
be	O
given	O
by	O
recursive	O
functions	O
,	O
algorithms	O
on	O
mutually	B-Algorithm
recursive	I-Algorithm
data	O
structures	O
can	O
be	O
naturally	O
given	O
by	O
mutually	B-Algorithm
recursive	I-Algorithm
functions	O
.	O
</s>
<s>
Common	O
examples	O
include	O
algorithms	O
on	O
trees	O
,	O
and	O
recursive	B-Application
descent	I-Application
parsers	I-Application
.	O
</s>
<s>
As	O
with	O
direct	B-Algorithm
recursion	I-Algorithm
,	O
tail	B-Language
call	I-Language
optimization	I-Language
is	O
necessary	O
if	O
the	O
recursion	O
depth	O
is	O
large	O
or	O
unbounded	O
,	O
such	O
as	O
using	O
mutual	B-Algorithm
recursion	I-Algorithm
for	O
multitasking	O
.	O
</s>
<s>
Note	O
that	O
tail	B-Language
call	I-Language
optimization	I-Language
in	O
general	O
(	O
when	O
the	O
function	O
called	O
is	O
not	O
the	O
same	O
as	O
the	O
original	O
function	O
,	O
as	O
in	O
tail-recursive	B-Language
calls	O
)	O
may	O
be	O
more	O
difficult	O
to	O
implement	O
than	O
the	O
special	O
case	O
of	O
tail-recursive	B-Language
call	O
optimization	O
,	O
and	O
thus	O
efficient	O
implementation	O
of	O
mutual	O
tail	B-Language
recursion	I-Language
may	O
be	O
absent	O
from	O
languages	O
that	O
only	O
optimize	O
tail-recursive	B-Language
calls	O
.	O
</s>
<s>
In	O
languages	O
such	O
as	O
Pascal	B-Application
that	O
require	O
declaration	O
before	O
use	O
,	O
mutually	B-Algorithm
recursive	I-Algorithm
functions	O
require	O
forward	O
declaration	O
,	O
as	O
a	O
forward	O
reference	O
cannot	O
be	O
avoided	O
when	O
defining	O
them	O
.	O
</s>
<s>
As	O
with	O
directly	O
recursive	O
functions	O
,	O
a	O
wrapper	O
function	O
may	O
be	O
useful	O
,	O
with	O
the	O
mutually	B-Algorithm
recursive	I-Algorithm
functions	O
defined	O
as	O
nested	O
functions	O
within	O
its	O
scope	O
if	O
this	O
is	O
supported	O
.	O
</s>
<s>
A	O
standard	O
example	O
of	O
mutual	B-Algorithm
recursion	I-Algorithm
,	O
which	O
is	O
admittedly	O
artificial	O
,	O
determines	O
whether	O
a	O
non-negative	O
number	O
is	O
even	O
or	O
odd	O
by	O
defining	O
two	O
separate	O
functions	O
that	O
call	O
each	O
other	O
,	O
decrementing	O
by	O
1	O
each	O
time	O
.	O
</s>
<s>
This	O
example	O
is	O
mutual	O
single	B-Algorithm
recursion	I-Algorithm
,	O
and	O
could	O
easily	O
be	O
replaced	O
by	O
iteration	O
.	O
</s>
<s>
In	O
this	O
example	O
,	O
the	O
mutually	B-Algorithm
recursive	I-Algorithm
calls	O
are	O
tail	B-Language
calls	I-Language
,	O
and	O
tail	B-Language
call	I-Language
optimization	I-Language
would	O
be	O
necessary	O
to	O
execute	O
in	O
constant	O
stack	O
space	O
.	O
</s>
<s>
As	O
a	O
more	O
general	O
class	O
of	O
examples	O
,	O
an	O
algorithm	O
on	O
a	O
tree	B-Application
can	O
be	O
decomposed	O
into	O
its	O
behavior	O
on	O
a	O
value	O
and	O
its	O
behavior	O
on	O
children	O
,	O
and	O
can	O
be	O
split	O
up	O
into	O
two	O
mutually	B-Algorithm
recursive	I-Algorithm
functions	O
,	O
one	O
specifying	O
the	O
behavior	O
on	O
a	O
tree	B-Application
,	O
calling	O
the	O
forest	O
function	O
for	O
the	O
forest	O
of	O
children	O
,	O
and	O
one	O
specifying	O
the	O
behavior	O
on	O
a	O
forest	O
,	O
calling	O
the	O
tree	B-Application
function	O
for	O
the	O
tree	B-Application
in	O
the	O
forest	O
.	O
</s>
<s>
In	O
this	O
case	O
the	O
tree	B-Application
function	O
calls	O
the	O
forest	O
function	O
by	O
single	B-Algorithm
recursion	I-Algorithm
,	O
but	O
the	O
forest	O
function	O
calls	O
the	O
tree	B-Application
function	O
by	O
multiple	B-Algorithm
recursion	I-Algorithm
.	O
</s>
<s>
Using	O
the	O
Standard	B-Language
ML	I-Language
datatype	O
above	O
,	O
the	O
size	O
of	O
a	O
tree	B-Application
(	O
number	O
of	O
nodes	O
)	O
can	O
be	O
computed	O
via	O
the	O
following	O
mutually	B-Algorithm
recursive	I-Algorithm
functions	O
:	O
</s>
<s>
A	O
more	O
detailed	O
example	O
in	O
Scheme	B-Language
,	O
counting	O
the	O
leaves	O
of	O
a	O
tree	B-Application
:	O
</s>
<s>
These	O
examples	O
reduce	O
easily	O
to	O
a	O
single	O
recursive	O
function	O
by	O
inlining	O
the	O
forest	O
function	O
in	O
the	O
tree	B-Application
function	O
,	O
which	O
is	O
commonly	O
done	O
in	O
practice	O
:	O
directly	O
recursive	O
functions	O
that	O
operate	O
on	O
trees	O
sequentially	O
process	O
the	O
value	O
of	O
the	O
node	O
and	O
recurse	O
on	O
the	O
children	O
within	O
one	O
function	O
,	O
rather	O
than	O
dividing	O
these	O
into	O
two	O
separate	O
functions	O
.	O
</s>
<s>
A	O
more	O
complicated	O
example	O
is	O
given	O
by	O
recursive	B-Application
descent	I-Application
parsers	I-Application
,	O
which	O
can	O
be	O
naturally	O
implemented	O
by	O
having	O
one	O
function	O
for	O
each	O
production	O
rule	O
of	O
a	O
grammar	O
,	O
which	O
then	O
mutually	O
recurse	O
;	O
this	O
will	O
in	O
general	O
be	O
multiple	B-Algorithm
recursion	I-Algorithm
,	O
as	O
production	O
rules	O
generally	O
combine	O
multiple	O
parts	O
.	O
</s>
<s>
This	O
can	O
also	O
be	O
done	O
without	O
mutual	B-Algorithm
recursion	I-Algorithm
,	O
for	O
example	O
by	O
still	O
having	O
separate	O
functions	O
for	O
each	O
production	O
rule	O
,	O
but	O
having	O
them	O
called	O
by	O
a	O
single	O
controller	O
function	O
,	O
or	O
by	O
putting	O
all	O
the	O
grammar	O
in	O
a	O
single	O
function	O
.	O
</s>
<s>
Mutual	B-Algorithm
recursion	I-Algorithm
can	O
also	O
implement	O
a	O
finite-state	B-Architecture
machine	I-Architecture
,	O
with	O
one	O
function	O
for	O
each	O
state	O
,	O
and	O
single	B-Algorithm
recursion	I-Algorithm
in	O
changing	O
state	O
;	O
this	O
requires	O
tail	B-Language
call	I-Language
optimization	I-Language
if	O
the	O
number	O
of	O
state	O
changes	O
is	O
large	O
or	O
unbounded	O
.	O
</s>
<s>
This	O
can	O
be	O
used	O
as	O
a	O
simple	O
form	O
of	O
cooperative	B-Operating_System
multitasking	I-Operating_System
.	O
</s>
<s>
A	O
similar	O
approach	O
to	O
multitasking	O
is	O
to	O
instead	O
use	O
coroutines	B-Architecture
which	O
call	O
each	O
other	O
,	O
where	O
rather	O
than	O
terminating	O
by	O
calling	O
another	O
routine	O
,	O
one	O
coroutine	B-Architecture
yields	O
to	O
another	O
but	O
does	O
not	O
terminate	O
,	O
and	O
then	O
resumes	O
execution	O
when	O
it	O
is	O
yielded	O
back	O
to	O
.	O
</s>
<s>
This	O
allows	O
individual	O
coroutines	B-Architecture
to	O
hold	O
state	O
,	O
without	O
it	O
needing	O
to	O
be	O
passed	O
by	O
parameters	O
or	O
stored	O
in	O
shared	O
variables	O
.	O
</s>
<s>
There	O
are	O
also	O
some	O
algorithms	O
which	O
naturally	O
have	O
two	O
phases	O
,	O
such	O
as	O
minimax	B-Algorithm
(	O
min	O
and	O
max	O
)	O
,	O
which	O
can	O
be	O
implemented	O
by	O
having	O
each	O
phase	O
in	O
a	O
separate	O
function	O
with	O
mutual	B-Algorithm
recursion	I-Algorithm
,	O
though	O
they	O
can	O
also	O
be	O
combined	O
into	O
a	O
single	O
function	O
with	O
direct	B-Algorithm
recursion	I-Algorithm
.	O
</s>
<s>
In	O
mathematics	O
,	O
the	O
Hofstadter	O
Female	O
and	O
Male	O
sequences	O
are	O
an	O
example	O
of	O
a	O
pair	O
of	O
integer	O
sequences	O
defined	O
in	O
a	O
mutually	B-Algorithm
recursive	I-Algorithm
manner	O
.	O
</s>
<s>
This	O
can	O
sometimes	O
be	O
done	O
more	O
elegantly	O
via	O
mutually	B-Algorithm
recursive	I-Algorithm
functions	O
;	O
the	O
Sierpiński	B-Algorithm
curve	I-Algorithm
is	O
a	O
good	O
example	O
.	O
</s>
<s>
Mutual	B-Algorithm
recursion	I-Algorithm
is	O
very	O
common	O
in	O
functional	B-Language
programming	I-Language
,	O
and	O
is	O
often	O
used	O
for	O
programs	O
written	O
in	O
LISP	B-Language
,	O
Scheme	B-Language
,	O
ML	B-Language
,	O
and	O
similar	O
programming	O
languages	O
.	O
</s>
<s>
For	O
example	O
,	O
Abelson	O
and	O
Sussman	O
describe	O
how	O
a	O
meta-circular	B-Application
evaluator	I-Application
can	O
be	O
used	O
to	O
implement	O
LISP	B-Language
with	O
an	O
eval-apply	O
cycle	O
.	O
</s>
<s>
In	O
languages	O
such	O
as	O
Prolog	B-Language
,	O
mutual	B-Algorithm
recursion	I-Algorithm
is	O
almost	O
unavoidable	O
.	O
</s>
<s>
Some	O
programming	O
styles	O
discourage	O
mutual	B-Algorithm
recursion	I-Algorithm
,	O
claiming	O
that	O
it	O
can	O
be	O
confusing	O
to	O
distinguish	O
the	O
conditions	O
which	O
will	O
return	O
an	O
answer	O
from	O
the	O
conditions	O
that	O
would	O
allow	O
the	O
code	O
to	O
run	O
forever	O
without	O
producing	O
an	O
answer	O
.	O
</s>
<s>
Mutual	B-Algorithm
recursion	I-Algorithm
is	O
also	O
known	O
as	O
indirect	B-Algorithm
recursion	I-Algorithm
,	O
by	O
contrast	O
with	O
direct	B-Algorithm
recursion	I-Algorithm
,	O
where	O
a	O
single	O
function	O
calls	O
itself	O
directly	O
.	O
</s>
<s>
This	O
is	O
simply	O
a	O
difference	O
of	O
emphasis	O
,	O
not	O
a	O
different	O
notion	O
:	O
"	O
indirect	B-Algorithm
recursion	I-Algorithm
"	O
emphasises	O
an	O
individual	O
function	O
,	O
while	O
"	O
mutual	B-Algorithm
recursion	I-Algorithm
"	O
emphasises	O
the	O
set	O
of	O
functions	O
,	O
and	O
does	O
not	O
single	O
out	O
an	O
individual	O
function	O
.	O
</s>
<s>
For	O
example	O
,	O
if	O
f	O
calls	O
itself	O
,	O
that	O
is	O
direct	B-Algorithm
recursion	I-Algorithm
.	O
</s>
<s>
Similarly	O
a	O
set	O
of	O
three	O
or	O
more	O
functions	O
that	O
call	O
each	O
other	O
can	O
be	O
called	O
a	O
set	O
of	O
mutually	B-Algorithm
recursive	I-Algorithm
functions	O
.	O
</s>
<s>
Mathematically	O
,	O
a	O
set	O
of	O
mutually	B-Algorithm
recursive	I-Algorithm
functions	O
are	O
primitive	B-Architecture
recursive	I-Architecture
,	O
which	O
can	O
be	O
proven	O
by	O
course-of-values	B-Algorithm
recursion	I-Algorithm
,	O
building	O
a	O
single	O
function	O
F	O
that	O
lists	O
the	O
values	O
of	O
the	O
individual	O
recursive	O
function	O
in	O
order	O
:	O
and	O
rewriting	O
the	O
mutual	B-Algorithm
recursion	I-Algorithm
as	O
a	O
primitive	B-Architecture
recursion	I-Architecture
.	O
</s>
<s>
Any	O
mutual	B-Algorithm
recursion	I-Algorithm
between	O
two	O
procedures	O
can	O
be	O
converted	O
to	O
direct	B-Algorithm
recursion	I-Algorithm
by	O
inlining	O
the	O
code	O
of	O
one	O
procedure	O
into	O
the	O
other	O
.	O
</s>
<s>
In	O
terms	O
of	O
the	O
call	O
stack	O
,	O
two	O
mutually	B-Algorithm
recursive	I-Algorithm
procedures	O
yield	O
a	O
stack	O
ABABAB	O
...	O
,	O
and	O
inlining	O
B	O
into	O
A	O
yields	O
the	O
direct	B-Algorithm
recursion	I-Algorithm
(	O
AB	O
)	O
(	O
AB	O
)	O
(	O
AB	O
)	O
...	O
</s>
<s>
Alternately	O
,	O
any	O
number	O
of	O
procedures	O
can	O
be	O
merged	O
into	O
a	O
single	O
procedure	O
that	O
takes	O
as	O
argument	O
a	O
variant	B-Language
record	I-Language
(	O
or	O
algebraic	O
data	O
type	O
)	O
representing	O
the	O
selection	O
of	O
a	O
procedure	O
and	O
its	O
arguments	O
;	O
the	O
merged	O
procedure	O
then	O
dispatches	O
on	O
its	O
argument	O
to	O
execute	O
the	O
corresponding	O
code	O
and	O
uses	O
direct	B-Algorithm
recursion	I-Algorithm
to	O
call	O
self	O
as	O
appropriate	O
.	O
</s>
<s>
This	O
can	O
be	O
seen	O
as	O
a	O
limited	O
application	O
of	O
defunctionalization	B-Application
.	O
</s>
<s>
This	O
translation	O
may	O
be	O
useful	O
when	O
any	O
of	O
the	O
mutually	B-Algorithm
recursive	I-Algorithm
procedures	O
can	O
be	O
called	O
by	O
outside	O
code	O
,	O
so	O
there	O
is	O
no	O
obvious	O
case	O
for	O
inlining	O
one	O
procedure	O
into	O
the	O
other	O
.	O
</s>
<s>
Such	O
code	O
then	O
needs	O
to	O
be	O
modified	O
so	O
that	O
procedure	O
calls	O
are	O
performed	O
by	O
bundling	O
arguments	O
into	O
a	O
variant	B-Language
record	I-Language
as	O
described	O
;	O
alternately	O
,	O
wrapper	O
procedures	O
may	O
be	O
used	O
for	O
this	O
task	O
.	O
</s>
