2011年7月31日
摘要: 什么是尾递归?简单来说就是最后返回的只是一个函数的调用,而不用保存多余的局部变量。看一个简单的计算阶乘的例子(Lua代码):function fact(n) return n==0 and 1 or n * fact(n-1)end 改成尾递归的方式就是:function tail_fact(n, p) p = p or 1 if n==0 then return p end return tail_fact(n-1, n*p)end 关于尾递归的更详细说明请参考: http://en.wikipedia.org/wiki/Tail_call因为使用尾递归方式的时候,是不用保存局部变量的了,所 阅读全文
posted @ 2011-07-31 01:52 Q.Lee.lulu 阅读(6836) 评论(5) 推荐(1) 编辑