What is tail-recursion

Consider a simple function that adds the first N integers.
(e.g. sum(5) = 1 + 2 + 3 + 4 + 5 = 15).

Here is a simple Python implementation that uses recursion:

def recsum(x):
if x == 1:
return x
else:
return x + recsum(x - 1)

If you called recsum(5), this is what the Python
interpreter would evaluate.


recsum(5)
5 + recsum(4)
5 + (4 + recsum(3))
5 + (4 + (3 + recsum(2)))
5 + (4 + (3 + (2 + recsum(1))))
5 + (4 + (3 + (2 + 1)))
15

Note how every recursive call has to complete before the Python interpreter
begins to actually do the work of calculating the sum.

Here‘s a tail-recursive version of the same function:

def tailrecsum(x, running_total=0):
if x == 0:
return running_total
else:
return tailrecsum(x - 1, running_total + x)

Here‘s the sequence of events that would occur if you
called tailrecsum(5), (which would effectively
be tailrecsum(5, 0), because of the default second
argument).


tailrecsum(5, 0)
tailrecsum(4, 5)
tailrecsum(3, 9)
tailrecsum(2, 12)
tailrecsum(1, 14)
tailrecsum(0, 15)
15

In the tail-recursive case, with each evaluation of the recursive call,
the running_total is updated.

时间: 2024-08-08 09:41:00

What is tail-recursion的相关文章

Scala Learning(3): Tail Recursion定义

关于尾递归 ,使用Scala的两个例子展示尾递归的定义和简单实现. 例子比较 求最大公约数的函数 def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) 计算的展开是尾递归的, gcd(14, 21) -> if (21 == 0) 14 else gcd(21, 14 % 21) -> if (false) 14 else gcd(21, 14 % 21) -> gcd(21, 14 % 21) -> gcd

Java Tail Recursion

Recursion.      /**      * sum from 1 to n. recursion      * @param i      * @return sum       */     public int recur_head(int i){         System.out.println("i = "+ i);         if(i==1)             return 1;         else             return i+r

Scala Tail Recursion (尾递归)

Scala对尾递归进行了优化,甚至提供了专门的标注告诉编译器需要进行尾递归优化.不过这种优化仅限于严格的尾递归,间接递归等情况,不会被优化. 尾递归的概念 递归,大家都不陌生,一个函数直接或间接的调用它自己,就是递归了.我们来看一个简单的,计算阶乘的例子. def factorial(n: Int): Int = { if( n <= 1 ) 1 else n * factorial(n-1) } 以上factorial方法,在n>1时,需要调用它自身,这是一个典型的递归调用.如果n=5,那么

数据结构与算法5: 递归(Recursion)

数据结构与算法5: 递归(Recursion) 写在前面 <软件随想录:程序员部落酋长Joel谈软件>一书中<学校只教java的危险性>一章提到,大学计算机系专业课有两个传统的知识点,但许多人从来都没搞懂过,那就是指针和递归.我也很遗憾没能早点熟练掌握这两个知识点.本节一些关键知识点和部分例子,都整理自教材或者网络,参考资料列在末尾.如果错误请纠正我. 思考列表: 1)什么程序具有递归解决的潜质? 2)递归还是非递归算法,怎么选择? 3)递归程序构造的一般模式 1.递归定义 首要引

scala tail recursive优化,复用函数栈

在scala中如果一个函数在最后一步调用自己(必须完全调用自己,不能加其他额外运算子),那么在scala中会复用函数栈,这样递归调用就转化成了线性的调用,效率大大的提高.If a function calls itself as its last action, the function's stack frame can be reused. This is called tail recursion.=> Tail recursive functions are iterative proc

python_factorial_tail recursion

目录 一.Create New Project 1.1 the rules of name 二.hugeng007_01_tail recursion 2.1 Conventional Recursive Factorial 三.The Unknown Word 一.Create New Project 1.1 the rules of name hugeng007_xx(number)_name 二.hugeng007_01_tail recursion 2.1 Conventional Re

php通过geohash算法实现查找附近的商铺

geohash有以下几个特点: 首先,geohash用一个字符串表示经度和纬度两个坐标.利用geohash,只需在一列上应用索引即可. 其次,geohash表示的并不是一个点,而是一个矩形区域.比如编码wx4g0ec19,它表示的是一个矩形区域. 使用者可以发布地址编码,既能表明自己位于北海公园附近,又不至于暴露自己的精确坐标,有助于隐私保护. 第三,编码的前缀可以表示更大的区域.例如wx4g0ec1,它的前缀wx4g0e表示包含编码wx4g0ec1在内的更大范围. 这个特性可以用于附近地点搜索

lua 高级篇(一)

第六章: 再论函数 Lua 中的函数是带有词法定界(lexical scoping)的第一类值(first-class values). 第一类值指:在 Lua 中函数和其他值(数值.字符串)一样,函数可以被存放在变                  量中,也可以存放在表中,可以作为函数的参数,还可以作为函数的返回值.词法定界指:被嵌套的函数可以访问他外部函数中的变量.这一特性给 Lua 提供了                 强大的编程能力. 先给出一些实例,看看lua的函数是如何跟普通变量一

Fibonacii非递归

记得在我们最开始学习C语言的时候,每当讲到递归,无论是课本上,还是老师,都会给出两个经典例子的递归实现,其中一个就是阶乘,另外一个就是Fibonacci(中文多译成斐波那契)数列了. 用递归方法计算阶乘的代码如下: //递归计算阶乘 long Factorial(int n) {     if (n <= 0)     {         return 1;     } return n * Factorial(n - 1); } 这样的递归是一个很明显的尾部递归的例子,所谓的尾部递归(tail

[email&#160;protected] find kth smallest element in two sorted arrays (O(log n time)

The trivial way, O(m + n): Merge both arrays and the k-th smallest element could be accessed directly. Merging would require extra space of O(m+n). The linear run time is pretty good, but could we improve it even further? A better way, O(k): There is