是尾递归吗? [英] is it tail recursion

查看:75
本文介绍了是尾递归吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int harmonic(int n){

if(n = 1){

返回1;

}

else {

返回谐波(n-1)+ 1 / n;

}

}

可以帮助我吗?

是Tail Rercursion吗?

i只想得到关于尾递归的答案和一些例子

尾部递归,这样我就可以区分尾部和非b / b
尾部累积。

int harmonic(int n) {
if (n=1) {
return 1;
}
else {
return harmonic(n-1)+1/n;
}
}

can any help me ??
Is it Tail Rercursion??
i just want the answer about tail recursion and and some examples
of tail recursion so that i can differentiate between tail and non-
tail recrsion.

推荐答案

您好,


我相信,根据定义,它不是。在第二种情况下,

函数不会返回_exactly_递归

调用返回的值(有一个必须添加和除法操作
在返回值之前执行



有关定义,请参阅:
http://www.google.com/search?hl=en&q...tail+recursion


问候。
Hello,

I believe that, by definition, it is not. In the second case the
function does not return _exactly_ the value returned by a recursive
call (there is an addition and a division operation that have to be
performed before returning the value).

For definitions, see:
http://www.google.com/search?hl=en&q...tail+recursion

Regards.


On 2008-11-03 03:55:06 -0500,Muzammil< mu ** ***********@gmail.comsaid:
On 2008-11-03 03:55:06 -0500, Muzammil <mu*************@gmail.comsaid:

int harmonic(int n){

if(n = 1){

返回1;

}

else {

返回谐波(n-1)+ 1 / n;

}

}


可以帮助我吗?

是吗Tail Rercursion ??

i只想得到关于尾递归的答案和一些例子

的尾递归让我可以区分尾巴和非尾巴之间的转换。
int harmonic(int n) {
if (n=1) {
return 1;
}
else {
return harmonic(n-1)+1/n;
}
}

can any help me ??
Is it Tail Rercursion??
i just want the answer about tail recursion and and some examples
of tail recursion so that i can differentiate between tail and non-
tail recrsion.



如果稍微更改最后一行,答案会更清晰:


返回1 / n +谐波(n -1);


由于函数的尾部是对函数的调用,这是尾部

递归。它可以被一个简单的循环取代,这是许多编译器所做的优化。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者

标准C ++库扩展:教程和参考

www.petebecker.com/tr1book

If you change the last line slightly the answer becomes a bit clearer:

return 1/n + harmonic(n-1);

Since the tail of the function is a call to the function, this is tail
recursion. It could be replaced by a simple loop, which is an
optimization that many compilers do.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


On 2008-11-03 06:18:44 -0500, al**********@gmail.com 说:
On 2008-11-03 06:18:44 -0500, al**********@gmail.com said:

您好,


我相信,根据定义,它不是。在第二种情况下,

函数不会返回_exactly_递归

调用返回的值(有一个必须添加和除法操作
在返回值之前执行



有关定义,请参阅:
http://www.google.com/search?hl=en&q...tail+recursion


问候。
Hello,

I believe that, by definition, it is not. In the second case the
function does not return _exactly_ the value returned by a recursive
call (there is an addition and a division operation that have to be
performed before returning the value).

For definitions, see:
http://www.google.com/search?hl=en&q...tail+recursion

Regards.



大多数编译器应该能够通过识别尾递归来优化原始代码到

循环。这不是返回

的问题,而是递归调用返回的值(这将是一个非常小的函数集,无用),但是是否可以将递归调用

更改为循环。一个例子不能是Ackerman的

函数:


int ackerman(unsigned i,unsigned j)

{

if(i == 0)

返回j + 1;

否则if(j == 0)

返回ackerman(i - 1,1);

else

返回ackerman(i - 1,ackerman(i,j - 1));

}


结果取决于多个递归调用,因此函数不能将
转换为一个简单的循环。它也快速增长;观看

溢出。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准的作者C ++库扩展:教程和参考

www.petebecker.com / tr1book

Most compilers ought to be able to optimize the original code into a
loop by recognizing the tail recursion. It''s not a matter of returning
exactly the value returned by the recursive call (that would be a very
small set of functions, all useless), but of whether the recursive call
can be changed into a loop. An example that can''t is Ackerman''s
function:

int ackerman(unsigned i, unsigned j)
{
if (i == 0)
return j + 1;
else if (j == 0)
return ackerman(i - 1, 1);
else
return ackerman(i - 1, ackerman(i, j - 1));
}

The result depends on multiple recursive calls, so the function can''t
be transformed into a simple loop. It also grows very quickly; watch
out for overflows.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


这篇关于是尾递归吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆