A
Anders J. Munch
Paul said:The usual compiler method is to translate the code into
continuation-passing style and thereby gain tail-recursion
optimization automagically.
There's no need to go into CPS just to optimise tail-recursion. After all,
compilers were optimising tail-calls decades before Appel's work on SML/NJ.
Converting tail-recursion to iteration is trivial, and perfectly reasonable for
a human to do by hand. You add an outer "while True"-loop, the recursive call
becomes a tuple assignment, and other code paths end with a break out of the
loop. Completely mechanical and the resulting code doesn't even look that bad.
Like Steven said, tail-call optimisation is not necessary as you can always
hand-optimise it yourself.
- Anders