在中
head recursion,递归调用在发生时先于函数中的其他处理(考虑它发生在函数的顶部或头部)。
在中
tail recursion,情况恰恰相反—处理发生在递归调用之前。在这两种递归样式之间进行选择似乎是任意的,但是选择可以使一切有所不同。
具有在路径的开头具有单个递归调用的路径的函数使用所谓的头递归。先前展览的阶乘功能使用头部递归。一旦确定需要递归,它所做的第一件事就是使用递减的参数进行调用。在路径末尾具有单个递归调用的函数正在使用尾递归。
请参阅这篇文章
递归示例:
public void tail(int n) | public void head(int n){ | { if(n == 1) | if(n == 0) return; | return; else | else System.out.println(n); | head(n-1); | tail(n-1); | System.out.println(n);} | }如果递归调用发生在方法的末尾,则称为
tail recursion。尾递归为
similar to a loop。的
method executesall the statements before jumping into the next recursive call。
如果递归调用发生在
beginning of a method, it is called a head recursion。的
method savesthe state before jumping into the next recursive call。



