What is tail recursion optimization?

Possible Duplicate:
What is tail-recursion?

What is tail recursion optimization?


a calls b calls c calls d.

at the end you have tails:

d returns to c returns to b returns to a. if all these do nothing (like in recursion, where it is actually a calls a calls a calls a) then you can optimize that... ...into d returns to a.


Tail recursion does not mean that you turn a recursive call into a loop.

It means that the stack frame is no longer necessary at the point of the recursive call, and therefore may be eliminated. This can occur if the recurisive call is the last statement in the method, and if the return value of the recursive call is the return value of the current method. By eliminating the current stack frame, recursive calls may go to arbitrary depth without stack-overflow errors and with substantial savings of resources.

This optimization is normally made by the compiler/interpreter rather than the programmer.


在JavaScript中:好的部分,Crockford说:“有些语言提供了尾递归优化,这意味着如果一个函数返回递归调用的结果,那么调用被循环替换,这可以显着提高速度。”

链接地址: http://www.djcxy.com/p/14162.html

上一篇: 另一个编码器被递归混淆

下一篇: 什么是尾递归优化?