Summary
-
An expression is in tail context if its value
will be returned immediately following its
evaluation.
-
A tail call is a function or method call that appears
in a tail context.
-
Whether tail calls are faster and more space-efficient
than non-tail calls depends on the implementation of
your programming language.
-
If the implementation is properly tail recursive,
tail calls are more space-efficient than
non-tail calls, and will probably be faster as well.
-
If the implementation is not properly tail recursive,
but performs tail call optimization, a tail call might
be more efficient than a non-tail call, but you can't
rely on that.
-
Some programming languages, notably Scheme, require
implementations to be properly tail recursive.
-
In most implementations of most programming languages,
tail calls are implemented so inefficiently
that tail calls are just as slow and consume just as
much space as non-tail calls.
-
The inefficiency of those implementations is unlikely
to be fixed in the near future.
-
In languages such as Java or C, whose implementations are
not properly tail recursive and may not even perform
tail call optimization, you'll need to rewrite
performance-sensitive tail calls using special loop
syntax.
-
Rewriting tail calls to use special syntax for loops
is not always possible, because the special syntax
is usually confined to a single function or method body.
With tail recursion, loops can cross function/method
boundaries.
For debugging:
Click here to validate.