[erlang-questions] Tail call optimization

Raimo Niskanen raimo+erlang-questions@REDACTED
Mon Oct 17 16:22:26 CEST 2016


On Mon, Oct 17, 2016 at 03:57:29PM +0200, Pierpaolo Bernardi wrote:
> On Mon, Oct 17, 2016 at 2:47 PM, Salikhov Dinislam
> <Dinislam.Salikhov@REDACTED> wrote:
> >> There is nothing about recursion in documentation.
> > The only doc that I've managed to find about the subject is the link from my
> > initial post
> > (http://erlang.org/doc/reference_manual/functions.html#id78464).
> > And it says about recursion: in sub-chapter's header, in sub-chapter itself
> > and in the example (all in all, everywhere). Is there another documentation
> > that you mean?
> 
> Yes, the chapter sub-header has 'recursion' in the name.
> 
> But the text says: "If the last expression of a function body is a
> function call, a tail recursive call is done."
> 
> This in no way can be read as meaning that when the last expression of
> a function body is a function call then a tail call is not mandated.
> 
> Maybe changing "tail recursive call" to "tail call" would remove an
> element of distraction and be more to the point though.

Yes!  We should change that detail in the documentation.  Recursion is not
a prerequisite for tail call optimization.

This is an implication of the fact that neither the compiler nor the VM,
due to hot code loading and due to that modules are compiled independently,
can depend on what a Module:Function call does (this time) so the tail call
optimization has to be done from the caller's side.

And it is hard to figure out a real use case where code depending on tail call
optimization does not eventually call back to itself hence use recursion.

So the tail call optimization is something the language depends hard on,
and the documentation is confusing when using the word "recursive" in this
context.  It probably comes from the course material where it talks about
"tail recursive" vs. "body recursive" calls, so this documentation probably
just wanted to use a familiar (but slightly incorrect) term...

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list