<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
tail	B-Language
call	I-Language
is	O
a	O
subroutine	O
call	O
performed	O
as	O
the	O
final	O
action	O
of	O
a	O
procedure	O
.	O
</s>
<s>
If	O
the	O
target	O
of	O
a	O
tail	O
is	O
the	O
same	O
subroutine	O
,	O
the	O
subroutine	O
is	O
said	O
to	O
be	O
tail	B-Language
recursive	I-Language
,	O
which	O
is	O
a	O
special	O
case	O
of	O
direct	O
recursion	O
.	O
</s>
<s>
Tail	B-Language
recursion	I-Language
(	O
or	O
tail-end	B-Language
recursion	I-Language
)	O
is	O
particularly	O
useful	O
,	O
and	O
is	O
often	O
easy	O
to	O
optimize	O
in	O
implementations	O
.	O
</s>
<s>
Tail	B-Language
calls	I-Language
can	O
be	O
implemented	O
without	O
adding	O
a	O
new	O
stack	O
frame	O
to	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
Most	O
of	O
the	O
frame	O
of	O
the	O
current	O
procedure	O
is	O
no	O
longer	O
needed	O
,	O
and	O
can	O
be	O
replaced	O
by	O
the	O
frame	O
of	O
the	O
tail	B-Language
call	I-Language
,	O
modified	O
as	O
appropriate	O
(	O
similar	O
to	O
overlay	B-Operating_System
for	O
processes	O
,	O
but	O
for	O
function	O
calls	O
)	O
.	O
</s>
<s>
The	O
program	O
can	O
then	O
jump	B-General_Concept
to	O
the	O
called	O
subroutine	O
.	O
</s>
<s>
Producing	O
such	O
code	O
instead	O
of	O
a	O
standard	O
call	O
sequence	O
is	O
called	O
tail-call	B-Language
elimination	I-Language
or	O
tail-call	B-Language
optimization	I-Language
.	O
</s>
<s>
Tail-call	B-Language
elimination	I-Language
allows	O
procedure	O
calls	O
in	O
tail	O
position	O
to	O
be	O
implemented	O
as	O
efficiently	O
as	O
goto	B-Application
statements	O
,	O
thus	O
allowing	O
efficient	B-General_Concept
structured	B-Language
programming	I-Language
.	O
</s>
<s>
In	O
the	O
words	O
of	O
Guy	O
L	O
.	O
Steele	O
,	O
"	O
in	O
general	O
,	O
procedure	O
calls	O
may	O
be	O
usefully	O
thought	O
of	O
as	O
GOTO	B-Application
statements	O
which	O
also	O
pass	O
parameters	O
,	O
and	O
can	O
be	O
uniformly	O
coded	O
as	O
[	O
machine	O
code ]	O
JUMP	B-General_Concept
instructions.	O
"	O
</s>
<s>
Not	O
all	O
programming	O
languages	O
require	O
tail-call	B-Language
elimination	I-Language
.	O
</s>
<s>
However	O
,	O
in	O
functional	B-Language
programming	I-Language
languages	I-Language
,	O
tail-call	B-Language
elimination	I-Language
is	O
often	O
guaranteed	O
by	O
the	O
language	O
standard	O
,	O
allowing	O
tail	B-Language
recursion	I-Language
to	O
use	O
a	O
similar	O
amount	O
of	O
memory	O
as	O
an	O
equivalent	O
loop	O
.	O
</s>
<s>
The	O
special	O
case	O
of	O
tail-recursive	B-Language
calls	O
,	O
when	O
a	O
function	O
calls	O
itself	O
,	O
may	O
be	O
more	O
amenable	O
to	O
call	O
elimination	O
than	O
general	O
tail	B-Language
calls	I-Language
.	O
</s>
<s>
When	O
the	O
language	O
semantics	O
do	O
not	O
explicitly	O
support	O
general	O
tail	B-Language
calls	I-Language
,	O
a	O
compiler	B-Language
can	O
often	O
still	O
optimize	O
sibling	O
calls	O
,	O
or	O
tail	B-Language
calls	I-Language
to	O
functions	O
which	O
take	O
and	O
return	O
the	O
same	O
types	O
as	O
the	O
caller	O
.	O
</s>
<s>
When	O
a	O
function	O
is	O
called	O
,	O
the	O
computer	O
must	O
"	O
remember	O
"	O
the	O
place	O
it	O
was	O
called	O
from	O
,	O
the	O
return	B-Language
address	I-Language
,	O
so	O
that	O
it	O
can	O
return	O
to	O
that	O
location	O
with	O
the	O
result	O
once	O
the	O
call	O
is	O
complete	O
.	O
</s>
<s>
Typically	O
,	O
this	O
information	O
is	O
saved	O
on	O
the	O
call	B-General_Concept
stack	I-General_Concept
,	O
a	O
simple	O
list	O
of	O
return	O
locations	O
in	O
order	O
of	O
the	O
times	O
that	O
the	O
call	O
locations	O
they	O
describe	O
were	O
reached	O
.	O
</s>
<s>
For	O
tail	B-Language
calls	I-Language
,	O
there	O
is	O
no	O
need	O
to	O
remember	O
the	O
caller	O
–	O
instead	O
,	O
tail-call	B-Language
elimination	I-Language
makes	O
only	O
the	O
minimum	O
necessary	O
changes	O
to	O
the	O
stack	O
frame	O
before	O
passing	O
it	O
on	O
,	O
and	O
the	O
tail-called	O
function	O
will	O
return	O
directly	O
to	O
the	O
original	O
caller	O
.	O
</s>
<s>
The	O
tail	B-Language
call	I-Language
does	O
n't	O
have	O
to	O
appear	O
lexically	O
after	O
all	O
other	O
statements	O
in	O
the	O
source	O
code	O
;	O
it	O
is	O
only	O
important	O
that	O
the	O
calling	O
function	O
return	O
immediately	O
after	O
the	O
tail	B-Language
call	I-Language
,	O
returning	O
the	O
tail	B-Language
call	I-Language
's	O
result	O
if	O
any	O
,	O
since	O
the	O
calling	O
function	O
is	O
bypassed	O
when	O
the	O
optimization	O
is	O
performed	O
.	O
</s>
<s>
When	O
dealing	O
with	O
recursive	O
or	O
mutually	B-Algorithm
recursive	I-Algorithm
functions	O
where	O
recursion	O
happens	O
through	O
tail	B-Language
calls	I-Language
,	O
however	O
,	O
the	O
stack	O
space	O
and	O
the	O
number	O
of	O
returns	O
saved	O
can	O
grow	O
to	O
be	O
very	O
significant	O
,	O
since	O
a	O
function	O
can	O
call	O
itself	O
,	O
directly	O
or	O
indirectly	O
,	O
creating	O
a	O
new	O
call	B-General_Concept
stack	I-General_Concept
frame	O
each	O
time	O
.	O
</s>
<s>
Tail-call	B-Language
elimination	I-Language
often	O
reduces	O
asymptotic	O
stack	O
space	O
requirements	O
from	O
linear	O
,	O
or	O
O(n )	O
,	O
to	O
constant	O
,	O
or	O
O(1 )	O
.	O
</s>
<s>
Tail-call	B-Language
elimination	I-Language
is	O
thus	O
required	O
by	O
the	O
standard	O
definitions	O
of	O
some	O
programming	O
languages	O
,	O
such	O
as	O
Scheme	B-Language
,	O
and	O
languages	O
in	O
the	O
ML	B-Language
family	O
among	O
others	O
.	O
</s>
<s>
The	O
Scheme	B-Language
language	I-Language
definition	O
formalizes	O
the	O
intuitive	O
notion	O
of	O
tail	O
position	O
exactly	O
,	O
by	O
specifying	O
which	O
syntactic	O
forms	O
allow	O
having	O
results	O
in	O
tail	O
context	O
.	O
</s>
<s>
Implementations	O
allowing	O
an	O
unlimited	O
number	O
of	O
tail	B-Language
calls	I-Language
to	O
be	O
active	O
at	O
the	O
same	O
moment	O
,	O
thanks	O
to	O
tail-call	B-Language
elimination	I-Language
,	O
can	O
also	O
be	O
called	O
'	O
properly	O
tail	B-Language
recursive	I-Language
 '	O
.	O
</s>
<s>
Besides	O
space	O
and	O
execution	O
efficiency	O
,	O
tail-call	B-Language
elimination	I-Language
is	O
important	O
in	O
the	O
functional	B-Language
programming	I-Language
idiom	O
known	O
as	O
continuation-passing	B-Application
style	I-Application
(	O
CPS	O
)	O
,	O
which	O
would	O
otherwise	O
quickly	O
run	O
out	O
of	O
stack	O
space	O
.	O
</s>
<s>
A	O
tail	B-Language
call	I-Language
can	O
be	O
located	O
just	O
before	O
the	O
syntactical	O
end	O
of	O
a	O
function	O
:	O
</s>
<s>
However	O
,	O
not	O
all	O
tail	B-Language
calls	I-Language
are	O
necessarily	O
located	O
at	O
the	O
syntactical	O
end	O
of	O
a	O
subroutine	O
:	O
</s>
<s>
Here	O
,	O
both	O
calls	O
to	O
b	O
and	O
c	B-Language
are	O
in	O
tail	O
position	O
.	O
</s>
<s>
the	O
call	O
to	O
a(data )	O
is	O
in	O
tail	O
position	O
in	O
foo2	O
,	O
but	O
it	O
is	O
not	O
in	O
tail	O
position	O
either	O
in	O
foo1	O
or	O
in	O
foo3	O
,	O
because	O
control	O
must	O
return	O
to	O
the	O
caller	O
to	O
allow	O
it	O
to	O
inspect	O
or	O
modify	O
the	O
return	B-Language
value	I-Language
before	O
returning	O
it	O
.	O
</s>
<s>
The	O
following	O
program	O
is	O
an	O
example	O
in	O
Scheme	B-Language
:	O
</s>
<s>
This	O
is	O
not	O
written	O
in	O
a	O
tail-recursive	B-Language
style	O
,	O
because	O
the	O
multiplication	O
function	O
("*"	O
)	O
is	O
in	O
the	O
tail	O
position	O
.	O
</s>
<s>
This	O
allows	O
an	O
interpreter	B-Application
or	O
compiler	B-Language
to	O
reorganize	O
the	O
execution	O
which	O
would	O
ordinarily	O
look	O
like	O
this	O
:	O
</s>
<s>
into	O
the	O
more	O
efficient	B-General_Concept
variant	O
,	O
in	O
terms	O
of	O
both	O
space	O
and	O
time	O
:	O
</s>
<s>
This	O
reorganization	O
saves	O
space	O
because	O
no	O
state	O
except	O
for	O
the	O
calling	O
function	O
's	O
address	O
needs	O
to	O
be	O
saved	O
,	O
either	O
on	O
the	O
stack	O
or	O
on	O
the	O
heap	O
,	O
and	O
the	O
call	B-General_Concept
stack	I-General_Concept
frame	O
for	O
fact-iter	O
is	O
reused	O
for	O
the	O
intermediate	O
results	O
storage	O
.	O
</s>
<s>
In	O
typical	O
implementations	O
,	O
the	O
tail-recursive	B-Language
variant	O
will	O
be	O
substantially	O
faster	O
than	O
the	O
other	O
variant	O
,	O
but	O
only	O
by	O
a	O
constant	O
factor	O
.	O
</s>
<s>
Some	O
programmers	O
working	O
in	O
functional	B-Language
languages	I-Language
will	O
rewrite	O
recursive	O
code	O
to	O
be	O
tail	B-Language
recursive	I-Language
so	O
they	O
can	O
take	O
advantage	O
of	O
this	O
feature	O
.	O
</s>
<s>
In	O
some	O
cases	O
(	O
such	O
as	O
filtering	O
lists	O
)	O
and	O
in	O
some	O
languages	O
,	O
full	O
tail	B-Language
recursion	I-Language
may	O
require	O
a	O
function	O
that	O
was	O
previously	O
purely	O
functional	B-Language
to	O
be	O
written	O
such	O
that	O
it	O
mutates	O
references	O
stored	O
in	O
other	O
variables	O
.	O
</s>
<s>
Tail	B-Language
recursion	I-Language
modulo	O
cons	B-Protocol
is	O
a	O
generalization	O
of	O
tail-recursion	B-Language
optimization	I-Language
introduced	O
by	O
David	O
H	O
.	O
D	O
.	O
Warren	O
in	O
the	O
context	O
of	O
compilation	B-Language
of	O
Prolog	B-Language
,	O
seen	O
as	O
an	O
explicitly	O
set	O
once	O
language	O
.	O
</s>
<s>
It	O
was	O
described	O
(	O
though	O
not	O
named	O
)	O
by	O
Daniel	O
P	O
.	O
Friedman	O
and	O
David	O
S	O
.	O
Wise	O
in	O
1974	O
as	O
a	O
LISP	B-Language
compilation	B-Language
technique	O
.	O
</s>
<s>
This	O
call	O
would	O
thus	O
be	O
a	O
tail	B-Language
call	I-Language
save	O
for	O
(	O
"	O
modulo	O
"	O
)	O
the	O
said	O
cons	B-Protocol
operation	O
.	O
</s>
<s>
The	O
following	O
Prolog	B-Language
fragment	O
illustrates	O
the	O
concept	O
:	O
</s>
<s>
%	O
Prolog	B-Language
,	O
tail	B-Language
recursive	I-Language
modulo	O
cons	B-Protocol
:	O
</s>
<s>
partition(Xs, Pivot, Smalls, Rest )	O
.--	O
In	O
Haskell	B-Language
,	O
guarded	O
recursion	O
:	O
</s>
<s>
(	O
a	O
,	O
b	O
)	O
=	O
partition	O
xs	O
p%	O
Prolog	B-Language
,	O
with	O
explicit	O
unifications	O
:	O
</s>
<s>
)	O
.	O
%	O
Prolog	B-Language
,	O
with	O
explicit	O
unifications	O
:	O
</s>
<s>
%	O
tail-recursive	B-Language
translation	O
:	O
</s>
<s>
Thus	O
in	O
tail-recursive	B-Language
translation	O
such	O
a	O
call	O
is	O
transformed	O
into	O
first	O
creating	O
a	O
new	O
list	B-Data_Structure
node	I-Data_Structure
and	O
setting	O
its	O
first	O
field	O
,	O
and	O
then	O
making	O
the	O
tail	B-Language
call	I-Language
with	O
the	O
pointer	O
to	O
the	O
node	O
's	O
rest	O
field	O
as	O
argument	O
,	O
to	O
be	O
filled	O
recursively	O
.	O
</s>
<s>
The	O
same	O
effect	O
is	O
achieved	O
when	O
the	O
recursion	O
is	O
guarded	O
under	O
a	O
lazily	O
evaluated	O
data	O
constructor	O
,	O
which	O
is	O
automatically	O
achieved	O
in	O
lazy	O
programming	O
languages	O
like	O
Haskell	B-Language
.	O
</s>
<s>
The	O
following	O
fragment	O
defines	O
a	O
recursive	O
function	O
in	O
C	B-Language
that	O
duplicates	O
a	O
linked	O
list	O
(	O
with	O
some	O
equivalent	O
Scheme	B-Language
and	O
Prolog	B-Language
code	O
as	O
comments	O
,	O
for	O
comparison	O
)	O
:	O
</s>
<s>
struct	B-Application
list	O
*	O
next	O
;	O
</s>
<s>
};	O
;	O
in	O
Scheme	B-Language
,	O
</s>
<s>
'	O
(	O
)	O
)	O
)	O
%%	O
in	O
Prolog	B-Language
,	O
</s>
<s>
In	O
this	O
form	O
the	O
function	O
is	O
not	O
tail	B-Language
recursive	I-Language
,	O
because	O
control	O
returns	O
to	O
the	O
caller	O
after	O
the	O
recursive	O
call	O
duplicates	O
the	O
rest	O
of	O
the	O
input	O
list	O
.	O
</s>
<s>
So	O
the	O
function	O
is	O
almost	O
tail	B-Language
recursive	I-Language
.	O
</s>
<s>
Warren	O
's	O
method	O
pushes	O
the	O
responsibility	O
of	O
filling	O
the	O
next	O
field	O
into	O
the	O
recursive	O
call	O
itself	O
,	O
which	O
thus	O
becomes	O
tail	B-Language
call	I-Language
.	O
</s>
<s>
struct	B-Application
list	O
*	O
next	O
;	O
</s>
<s>
};	O
;	O
in	O
Scheme	B-Language
,	O
</s>
<s>
(	O
cdr	O
head	O
)	O
)	O
)	O
%%	O
in	O
Prolog	B-Language
,	O
</s>
<s>
It	O
is	O
thus	O
similar	O
to	O
the	O
accumulating	O
parameter	O
technique	O
,	O
turning	O
a	O
recursive	O
computation	O
into	O
an	O
iterative	B-Algorithm
one	O
.	O
</s>
<s>
Characteristically	O
for	O
this	O
technique	O
,	O
a	O
parent	O
frame	O
is	O
created	O
on	O
the	O
execution	O
call	B-General_Concept
stack	I-General_Concept
,	O
which	O
the	O
tail-recursive	B-Language
callee	O
can	O
reuse	O
as	O
its	O
own	O
call	O
frame	O
if	O
the	O
tail-call	B-Language
optimization	I-Language
is	O
present	O
.	O
</s>
<s>
The	O
tail-recursive	B-Language
implementation	O
can	O
now	O
be	O
converted	O
into	O
an	O
explicitly	O
iterative	B-Algorithm
implementation	O
,	O
as	O
an	O
accumulating	O
loop	O
:	O
</s>
<s>
struct	B-Application
list	O
*	O
next	O
;	O
</s>
<s>
}	O
;;	O
in	O
Scheme	B-Language
,	O
</s>
<s>
end	O
(	O
list	O
(	O
car	O
ls	O
)	O
)	O
)	O
)	O
)	O
)	O
%%	O
in	O
Prolog	B-Language
,	O
</s>
<s>
In	O
a	O
paper	O
delivered	O
to	O
the	O
ACM	O
conference	O
in	O
Seattle	O
in	O
1977	O
,	O
Guy	O
L	O
.	O
Steele	O
summarized	O
the	O
debate	O
over	O
the	O
GOTO	B-Application
and	O
structured	B-Language
programming	I-Language
,	O
and	O
observed	O
that	O
procedure	O
calls	O
in	O
the	O
tail	O
position	O
of	O
a	O
procedure	O
can	O
be	O
best	O
treated	O
as	O
a	O
direct	O
transfer	O
of	O
control	O
to	O
the	O
called	O
procedure	O
,	O
typically	O
eliminating	O
unnecessary	O
stack	O
manipulation	O
operations	O
.	O
</s>
<s>
Since	O
such	O
"	O
tail	B-Language
calls	I-Language
"	O
are	O
very	O
common	O
in	O
Lisp	B-Language
,	O
a	O
language	O
where	O
procedure	O
calls	O
are	O
ubiquitous	O
,	O
this	O
form	O
of	O
optimization	O
considerably	O
reduces	O
the	O
cost	O
of	O
a	O
procedure	O
call	O
compared	O
to	O
other	O
implementations	O
.	O
</s>
<s>
Steele	O
argued	O
that	O
poorly-implemented	O
procedure	O
calls	O
had	O
led	O
to	O
an	O
artificial	O
perception	O
that	O
the	O
GOTO	B-Application
was	O
cheap	O
compared	O
to	O
the	O
procedure	O
call	O
.	O
</s>
<s>
Steele	O
further	O
argued	O
that	O
"	O
in	O
general	O
procedure	O
calls	O
may	O
be	O
usefully	O
thought	O
of	O
as	O
GOTO	B-Application
statements	O
which	O
also	O
pass	O
parameters	O
,	O
and	O
can	O
be	O
uniformly	O
coded	O
as	O
[	O
machine	O
code ]	O
JUMP	B-General_Concept
instructions	O
"	O
,	O
with	O
the	O
machine	O
code	O
stack	O
manipulation	O
instructions	O
"	O
considered	O
an	O
optimization	O
(	O
rather	O
than	O
vice	O
versa	O
!	O
)	O
"	O
.	O
</s>
<s>
Steele	O
cited	O
evidence	O
that	O
well-optimized	O
numerical	O
algorithms	O
in	O
Lisp	B-Language
could	O
execute	O
faster	O
than	O
code	O
produced	O
by	O
then-available	O
commercial	O
Fortran	O
compilers	B-Language
because	O
the	O
cost	O
of	O
a	O
procedure	O
call	O
in	O
Lisp	B-Language
was	O
much	O
lower	O
.	O
</s>
<s>
In	O
Scheme	B-Language
,	O
a	O
Lisp	B-Language
dialect	O
developed	O
by	O
Steele	O
with	O
Gerald	O
Jay	O
Sussman	O
,	O
tail-call	B-Language
elimination	I-Language
is	O
guaranteed	O
to	O
be	O
implemented	O
in	O
any	O
interpreter	B-Application
.	O
</s>
<s>
Tail	B-Language
recursion	I-Language
is	O
important	O
to	O
some	O
high-level	B-Language
languages	I-Language
,	O
especially	O
functional	B-Language
and	O
logic	B-Language
languages	I-Language
and	O
members	O
of	O
the	O
Lisp	B-Language
family	O
.	O
</s>
<s>
In	O
these	O
languages	O
,	O
tail	B-Language
recursion	I-Language
is	O
the	O
most	O
commonly	O
used	O
way	O
(	O
and	O
sometimes	O
the	O
only	O
way	O
available	O
)	O
of	O
implementing	O
iteration	B-Algorithm
.	O
</s>
<s>
The	O
language	O
specification	O
of	O
Scheme	B-Language
requires	O
that	O
tail	B-Language
calls	I-Language
are	O
to	O
be	O
optimized	O
so	O
as	O
not	O
to	O
grow	O
the	O
stack	O
.	O
</s>
<s>
Tail	B-Language
calls	I-Language
can	O
be	O
made	O
explicitly	O
in	O
Perl	B-Language
,	O
with	O
a	O
variant	O
of	O
the	O
"	O
goto	B-Application
"	O
statement	O
that	O
takes	O
a	O
function	O
name	O
:	O
goto	B-Application
&	O
NAME	O
;	O
</s>
<s>
However	O
,	O
for	O
language	O
implementations	O
which	O
store	O
function	O
arguments	O
and	O
local	O
variables	O
on	O
a	O
call	B-General_Concept
stack	I-General_Concept
(	O
which	O
is	O
the	O
default	O
implementation	O
for	O
many	O
languages	O
,	O
at	O
least	O
on	O
systems	O
with	O
a	O
hardware	O
stack	O
,	O
such	O
as	O
the	O
x86	B-Operating_System
)	O
,	O
implementing	O
generalized	O
tail-call	B-Language
optimization	I-Language
(	O
including	O
mutual	O
tail	B-Language
recursion	I-Language
)	O
presents	O
an	O
issue	O
:	O
if	O
the	O
size	O
of	O
the	O
callee	O
's	O
activation	O
record	O
is	O
different	O
from	O
that	O
of	O
the	O
caller	O
,	O
then	O
additional	O
cleanup	O
or	O
resizing	O
of	O
the	O
stack	O
frame	O
may	O
be	O
required	O
.	O
</s>
<s>
For	O
these	O
cases	O
,	O
optimizing	O
tail	B-Language
recursion	I-Language
remains	O
trivial	O
,	O
but	O
general	O
tail-call	B-Language
optimization	I-Language
may	O
be	O
harder	O
to	O
implement	O
efficiently	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
the	O
Java	B-Language
virtual	I-Language
machine	I-Language
(	O
JVM	B-Language
)	O
,	O
tail-recursive	B-Language
calls	O
can	O
be	O
eliminated	O
(	O
as	O
this	O
reuses	O
the	O
existing	O
call	B-General_Concept
stack	I-General_Concept
)	O
,	O
but	O
general	O
tail	B-Language
calls	I-Language
cannot	O
be	O
(	O
as	O
this	O
changes	O
the	O
call	B-General_Concept
stack	I-General_Concept
)	O
.	O
</s>
<s>
As	O
a	O
result	O
,	O
functional	B-Language
languages	I-Language
such	O
as	O
Scala	B-Application
that	O
target	O
the	O
JVM	B-Language
can	O
efficiently	O
implement	O
direct	O
tail	B-Language
recursion	I-Language
,	O
but	O
not	O
mutual	O
tail	B-Language
recursion	I-Language
.	O
</s>
<s>
The	O
GCC	B-Application
,	O
LLVM/Clang	B-Application
,	O
and	O
Intel	B-Language
compiler	B-Language
suites	O
perform	O
tail-call	B-Language
optimization	I-Language
for	O
C	B-Language
and	O
other	O
languages	O
at	O
higher	O
optimization	O
levels	O
or	O
when	O
the	O
-foptimize-sibling-calls	O
option	O
is	O
passed	O
.	O
</s>
<s>
Though	O
the	O
given	O
language	O
syntax	O
may	O
not	O
explicitly	O
support	O
it	O
,	O
the	O
compiler	B-Language
can	O
make	O
this	O
optimization	O
whenever	O
it	O
can	O
determine	O
that	O
the	O
return	O
types	O
for	O
the	O
caller	O
and	O
callee	O
are	O
equivalent	O
,	O
and	O
that	O
the	O
argument	O
types	O
passed	O
to	O
both	O
function	O
are	O
either	O
the	O
same	O
,	O
or	O
require	O
the	O
same	O
amount	O
of	O
total	O
storage	O
space	O
on	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
Tail	B-Language
calls	I-Language
are	O
often	O
optimized	O
by	O
interpreters	B-Application
and	O
compilers	B-Language
of	O
functional	B-Language
programming	I-Language
and	O
logic	B-Language
programming	I-Language
languages	I-Language
to	O
more	O
efficient	B-General_Concept
forms	O
of	O
iteration	B-Algorithm
.	O
</s>
<s>
For	O
example	O
,	O
Scheme	B-Language
programmers	O
commonly	O
express	O
while	O
loops	O
as	O
calls	O
to	O
procedures	O
in	O
tail	O
position	O
and	O
rely	O
on	O
the	O
Scheme	B-Language
compiler	B-Language
or	O
interpreter	B-Application
to	O
substitute	O
the	O
tail	B-Language
calls	I-Language
with	O
more	O
efficient	B-General_Concept
jump	B-General_Concept
instructions	O
.	O
</s>
<s>
For	O
compilers	B-Language
generating	O
assembly	O
directly	O
,	O
tail-call	B-Language
elimination	I-Language
is	O
easy	O
:	O
it	O
suffices	O
to	O
replace	O
a	O
call	O
opcode	O
with	O
a	O
jump	B-General_Concept
one	O
,	O
after	O
fixing	O
parameters	O
on	O
the	O
stack	O
.	O
</s>
<s>
From	O
a	O
compiler	B-Language
's	O
perspective	O
,	O
the	O
first	O
example	O
above	O
is	O
initially	O
translated	O
into	O
pseudo-assembly	O
language	O
(	O
in	O
fact	O
,	O
this	O
is	O
valid	O
x86	B-Language
assembly	I-Language
)	O
:	O
</s>
<s>
Tail-call	B-Language
elimination	I-Language
replaces	O
the	O
last	O
two	O
lines	O
with	O
a	O
single	O
jump	B-General_Concept
instruction	O
:	O
</s>
<s>
After	O
subroutine	O
A	O
completes	O
,	O
it	O
will	O
then	O
return	O
directly	O
to	O
the	O
return	B-Language
address	I-Language
of	O
foo	O
,	O
omitting	O
the	O
unnecessary	O
ret	O
statement	O
.	O
</s>
<s>
For	O
instance	O
,	O
on	O
platforms	B-Device
where	O
the	O
call	B-General_Concept
stack	I-General_Concept
does	O
not	O
just	O
contain	O
the	O
return	B-Language
address	I-Language
,	O
but	O
also	O
the	O
parameters	O
for	O
the	O
subroutine	O
,	O
the	O
compiler	B-Language
may	O
need	O
to	O
emit	O
instructions	O
to	O
adjust	O
the	O
call	B-General_Concept
stack	I-General_Concept
.	O
</s>
<s>
On	O
such	O
a	O
platform	B-Device
,	O
for	O
the	O
code	O
:	O
</s>
<s>
(	O
where	O
data1	O
and	O
data2	O
are	O
parameters	O
)	O
a	O
compiler	B-Language
might	O
translate	O
that	O
as	O
:	O
</s>
<s>
This	O
code	O
is	O
more	O
efficient	B-General_Concept
both	O
in	O
terms	O
of	O
execution	O
speed	O
and	O
use	O
of	O
stack	O
space	O
.	O
</s>
<s>
Since	O
many	O
Scheme	B-Language
compilers	B-Language
use	O
C	B-Language
as	O
an	O
intermediate	O
target	O
code	O
,	O
the	O
tail	B-Language
recursion	I-Language
must	O
be	O
encoded	O
in	O
C	B-Language
without	O
growing	O
the	O
stack	O
,	O
even	O
if	O
the	O
C	B-Language
compiler	B-Language
does	O
not	O
optimize	O
tail	B-Language
calls	I-Language
.	O
</s>
<s>
Many	O
implementations	O
achieve	O
this	O
by	O
using	O
a	O
device	O
known	O
as	O
a	O
trampoline	B-General_Concept
,	O
a	O
piece	O
of	O
code	O
that	O
repeatedly	O
calls	O
functions	O
.	O
</s>
<s>
All	O
functions	O
are	O
entered	O
via	O
the	O
trampoline	B-General_Concept
.	O
</s>
<s>
When	O
a	O
function	O
has	O
to	O
tail-call	O
another	O
,	O
instead	O
of	O
calling	O
it	O
directly	O
and	O
then	O
returning	O
the	O
result	O
,	O
it	O
returns	O
the	O
address	O
of	O
the	O
function	O
to	O
be	O
called	O
and	O
the	O
call	O
parameters	O
back	O
to	O
the	O
trampoline	B-General_Concept
(	O
from	O
which	O
it	O
was	O
called	O
itself	O
)	O
,	O
and	O
the	O
trampoline	B-General_Concept
takes	O
care	O
of	O
calling	O
this	O
function	O
next	O
with	O
the	O
specified	O
parameters	O
.	O
</s>
<s>
This	O
ensures	O
that	O
the	O
C	B-Language
stack	O
does	O
not	O
grow	O
and	O
iteration	B-Algorithm
can	O
continue	O
indefinitely	O
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
implement	O
trampolines	B-General_Concept
using	O
higher-order	B-Language
functions	I-Language
in	O
languages	O
that	O
support	O
them	O
,	O
such	O
as	O
Groovy	B-Application
,	O
Visual	B-Language
Basic	I-Language
.NET	I-Language
and	O
C#	B-Application
.	O
</s>
<s>
Using	O
a	O
trampoline	B-General_Concept
for	O
all	O
function	O
calls	O
is	O
rather	O
more	O
expensive	O
than	O
the	O
normal	O
C	B-Language
function	O
call	O
,	O
so	O
at	O
least	O
one	O
Scheme	B-Language
compiler	B-Language
,	O
Chicken	B-Language
,	O
uses	O
a	O
technique	O
first	O
described	O
by	O
Henry	O
Baker	O
from	O
an	O
unpublished	O
suggestion	O
by	O
Andrew	O
Appel	O
,	O
in	O
which	O
normal	O
Ccalls	O
are	O
used	O
but	O
the	O
stack	O
size	O
is	O
checked	O
before	O
every	O
call	O
.	O
</s>
<s>
When	O
the	O
stack	O
reaches	O
its	O
maximum	O
permitted	O
size	O
,	O
objects	O
on	O
the	O
stack	O
are	O
garbage-collected	B-General_Concept
using	O
the	O
Cheney	B-General_Concept
algorithm	I-General_Concept
by	O
moving	O
all	O
live	O
data	O
into	O
a	O
separate	O
heap	O
.	O
</s>
<s>
Following	O
this	O
,	O
the	O
stack	O
is	O
unwound	O
(	O
"	O
popped	O
"	O
)	O
and	O
the	O
program	O
resumes	O
from	O
the	O
state	O
saved	O
just	O
before	O
the	O
garbage	B-General_Concept
collection	I-General_Concept
.	O
</s>
<s>
Baker	O
says	O
"	O
Appel	O
's	O
method	O
avoids	O
making	O
a	O
large	O
number	O
of	O
small	O
trampoline	B-General_Concept
bounces	O
by	O
occasionally	O
jumping	O
off	O
the	O
Empire	O
State	O
Building.	O
"	O
</s>
<s>
The	O
garbage	B-General_Concept
collection	I-General_Concept
ensures	O
that	O
mutual	O
tail	B-Language
recursion	I-Language
can	O
continue	O
indefinitely	O
.	O
</s>
<s>
However	O
,	O
this	O
approach	O
requires	O
that	O
no	O
C	B-Language
function	O
call	O
ever	O
returns	O
,	O
since	O
there	O
is	O
no	O
guarantee	O
that	O
its	O
caller	O
's	O
stack	O
frame	O
still	O
exists	O
;	O
therefore	O
,	O
it	O
involves	O
a	O
much	O
more	O
dramatic	O
internal	O
rewriting	O
of	O
the	O
program	O
code	O
:	O
continuation-passing	B-Application
style	I-Application
.	O
</s>
<s>
For	O
instance	O
,	O
this	O
Julia	B-Application
program	O
gives	O
a	O
non-tail	O
recursive	O
definition	O
fact	O
of	O
the	O
factorial	O
:	O
</s>
<s>
But	O
it	O
can	O
be	O
transformed	O
into	O
a	O
tail-recursive	B-Language
definition	O
by	O
adding	O
an	O
argument	O
a	O
called	O
an	O
accumulator	O
.	O
</s>
<s>
This	O
Julia	B-Application
program	O
gives	O
a	O
tail-recursive	B-Language
definition	O
fact_iter	O
of	O
the	O
factorial	O
:	O
</s>
<s>
This	O
Julia	B-Application
program	O
gives	O
an	O
iterative	B-Algorithm
definition	O
fact_iter	O
of	O
the	O
factorial	O
:	O
</s>
<s>
Clojure	B-Language
Clojure	B-Language
has	O
recur	O
special	O
form	O
.	O
</s>
<s>
Elixir	B-Language
Elixir	B-Language
implements	O
tail-call	B-Language
optimization	I-Language
,	O
as	O
do	O
all	O
languages	O
currently	O
targeting	O
the	O
BEAM	O
VM	O
.	O
</s>
<s>
Objective-C	B-Language
Compiler	B-Language
optimizes	O
tail	B-Language
calls	I-Language
when	O
-O1	O
(	O
or	O
higher	O
)	O
option	O
specified	O
but	O
it	O
is	O
easily	O
disturbed	O
by	O
calls	O
added	O
by	O
Automatic	B-Language
Reference	I-Language
Counting	I-Language
(	O
ARC	O
)	O
.	O
</s>
<s>
Perl	B-Language
Explicit	O
with	O
a	O
variant	O
of	O
the	O
"	O
goto	B-Application
"	O
statement	O
that	O
takes	O
a	O
function	O
name	O
:	O
goto	B-Application
&	O
NAME	O
;	O
</s>
<s>
Python	B-Language
Stock	O
Python	B-Language
implementations	O
do	O
not	O
perform	O
tail-call	B-Language
optimization	I-Language
,	O
though	O
a	O
third-party	O
module	O
is	O
available	O
to	O
do	O
this	O
.	O
</s>
<s>
Scala	B-Application
Tail-recursive	B-Language
functions	I-Language
are	O
automatically	O
optimized	O
by	O
the	O
compiler	B-Language
.	O
</s>
