Programming Recursion
Comprox
*chortle*Canada Join Date: 2002-01-23 Member: 7Members, Super Administrators, Forum Admins, NS1 Playtester, NS2 Developer, Constellation, NS2 Playtester, Reinforced - Shadow, WC 2013 - Silver, Subnautica Developer, Subnautica Playtester, Pistachionauts
in Off-Topic
Ok, I am trying to understand recursion, I have seen it on and off for a few years, but it still makes no sense to me.
In an attempt to understand it more, I'm doing some practice problems, one to calculate factorials (ie. 5!) and one to reverse strings of text in preparation for a forthcoming exam. I got the factorial one after some work, but am totally stumped on the string one. What is supposed to happen is it converts 'hello' to 'olleh' using recursion and substring calls, but I've gotten everything from 'ooooo' to 'eeehhhlllllllloooo'. Could anyone maybe get me off on the right foot here with some suggestions?
In an attempt to understand it more, I'm doing some practice problems, one to calculate factorials (ie. 5!) and one to reverse strings of text in preparation for a forthcoming exam. I got the factorial one after some work, but am totally stumped on the string one. What is supposed to happen is it converts 'hello' to 'olleh' using recursion and substring calls, but I've gotten everything from 'ooooo' to 'eeehhhlllllllloooo'. Could anyone maybe get me off on the right foot here with some suggestions?
Comments
You should be able to figure it out from that.
[Edit]: From an implementation standpoint I believe it's easier to take the letter off the end rather than the beginning
I may have an old Java prog that does the string thing.
thanks <!--emo&:D--><img src='http://www.unknownworlds.com/forums/html/emoticons/biggrin-fix.gif' border='0' style='vertical-align:middle' alt='biggrin-fix.gif' /><!--endemo-->
I learned it via doing a bubble sort in asembly language.
probably an easy way (though not %100 correct) to think about it is a string of commands, one of those commands being a GOTO 1 (bad programing, I know, but that is how I learned it)
It also dosn't have to be the last step, so often a good part of the recursion happens as you are steping out of the loop (so you do a string of actions as you step in, and then once the recursion is done you perform a different set of actions, in reverse order to the data)
<!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->
First we have all of the function calls. This means that whenever a piece of data needs further action, it calls itself.
factorial(5); Subtract1, send to itself.
|4
|_Factorial(4); Subtract1, send to itself.
|3
|_Factorial(3); Subtract1, send to itself.
|3
|_Factorial(2); Subtract1, send to itself.
|2
|_Factorial(1); STOP. This means that no further calls are needed. HERE is where the computation actually starts happening and numbers are returned rather than sent.
Now collapse the functions as such, working from bottom to top:
Return 24*5=96;
|
|_Return 6*4=24;
|
|_Return 2*3=6;
|
|_Return 1*2=2; (Based on the statement Return number*Factorial(number-1))
|
|_Return 1;
<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
Recursion. Adj. Recursion.
(I may have messed up somewhere.)
<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Gentlemen... welcome to recursion club. The first rule is: you do not talk about recursion club. The next rule is: see first rule.<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Gentlemen... welcome to recursion club. The first rule is: you do not talk about recursion club. The next rule is: see first rule.<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--> <!--QuoteEnd--> </td></tr></table><div class='postcolor'> <!--QuoteEEnd-->
As much as I enjoyed that as well, thats not a recursion.
(I dont wanna be an arse, but this may confuse people.)
Hehe. Sorry, couldn't resist. :P
You need a base case, otherwise it defeats the whole point of using recursion (unless your point is to crash the machine, of course).
Like a-this:
I think that<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I think that<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I think that<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I think that <!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I think that<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I think that<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I think that<!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->I think, therefore I am.<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd--><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->