Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: recursion basicsby robby_dobby (Hermit) |
on Jun 23, 2015 at 06:41 UTC ( [id://1131571]=note: print w/replies, xml ) | Need Help?? |
Hello wrinkles,
The thing about recursion is, (and this is what trips up most learners) it does NOT compute results UNTIL it has reached the terminating condition. That is, it would go on accumulating non-computed results and builds the final value once it reaches the end of the recursion condition. Now, it should be seen that for recursion to happen - it always needs two factors in place:
Hopefully, I can make this clear by using a well known factorial example.
Now, consider what would happen if you were to invoke it as: factorial(5). You have the terminating condition and the means to compute the next thunk. It should look something like below: factorial(5) 5 * factorial(4) 5 * 4 * factorial(3) 5 * 4 * 3 * factorial(2) 5 * 4 * 3 * 2 * factorial(1) 5 * 4 * 3 * 2 * 1 = 120 Understood so far? Let's take a look at binary:
Hopefully, it's all a bit more clear now. You can read more about recursion on Wikipedia here.
In Section
Seekers of Perl Wisdom
|
|