Thinking recursively
First,Let's know the principle:
Recursive leap of faith-
When you try to understand a recursive program,you must be able to put the underlying details aside and focus instead on a single level of the operation. At that level,you are allowed to assume that any recursive call automatically gets the right answer as long as the arguments to that call are simpler than the original arguments in some respect.The psychological strategy-assuming that any simpler recursive call will work correctly-is called the recursive leap of faith!
The idea may be difficult to newers! Take an example for it:
We all know the Fibonacci function:
F(n)=F(n-1)+F(n-2)
Recursive implementation of the Fibonacci funtion:
int
Fib(
int
n)
{
if (n<=1)
return n;
else
return Fib(n-1)+Fib(n-2);
}
if n is 5,Fib(5) is computed by the sum of Fib(4) and Fib(3).
Applying the faith,you can assume that the program correctly computes each of these values,without going through all the steps that Fib(4) and Fib(3) is computed!
本文介绍了递归编程的基本原理,特别是递归信念的概念,并通过斐波那契数列的实例展示了如何应用这一理念来简化问题并解决复杂的递归调用。

2443

被折叠的 条评论
为什么被折叠?



