Programming Recursion

ComproxComprox *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
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?

Comments

  • JHunzJHunz Join Date: 2002-11-15 Member: 8815Members, Constellation
    edited January 2005
    Basically what you want to do in this problem is write a function that takes the first letter of the string, makes a recursive call to itself with the string minus that letter, then adds that letter on the end of the result.

    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
  • SnidelySnidely Join Date: 2003-02-04 Member: 13098Members
    I know recursion is the same idea for most languages, but it would help if you specified. C++? Java? Prolog?

    I may have an old Java prog that does the string thing.
  • ComproxComprox *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
    Jhunz approval++;

    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-->
  • ThansalThansal The New Scum Join Date: 2002-08-22 Member: 1215Members, Constellation
    hehe, recursion, one of the greatest things on earth, and confusing as hell untill it finaly snaps for yah.

    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)
  • NumbersNotFoundNumbersNotFound Join Date: 2002-11-07 Member: 7556Members
    The way i think about it is through a tree of sorts:



    <!--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.)
  • Marik_SteeleMarik_Steele To rule in hell... Join Date: 2002-11-20 Member: 9466Members
    For once, <a href='http://bash.org/?444349' target='_blank'>a specific Bash.org quote</a> is both inoffensive <i>and</i> relevant!
    <!--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-->
  • Umbraed_MonkeyUmbraed_Monkey Join Date: 2002-11-25 Member: 9922Members
    <!--QuoteBegin-Marik Steele+Jan 19 2005, 06:06 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Marik Steele @ Jan 19 2005, 06:06 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> For once, <a href='http://bash.org/?444349' target='_blank'>a specific Bash.org quote</a> is both inoffensive <i>and</i> relevant!
    <!--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.)
  • [WHO]Them[WHO]Them You can call me Dave Join Date: 2002-12-11 Member: 10593Members, Constellation
    Remember, the stack is not a plaything.
  • funbagsfunbags Join Date: 2003-06-08 Member: 17099Members
    So who here knows C++ really well? I mean, enough to program for say...a half life 2 mod?
  • DOOManiacDOOManiac Worst. Critic. Ever. Join Date: 2002-04-17 Member: 462Members, NS1 Playtester
    edited January 2005
    Recursion is all about<!--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-->Recursion is all about<!--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-->Recursion is all about<!--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-->Recursion is all about<!--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-->Recursion is all about<!--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-->Recursion is all about<!--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-->Recursion is all about<!--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-->Recursion is all about<!--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-->






    Hehe. Sorry, couldn't resist. :P
  • SoulSkorpionSoulSkorpion Join Date: 2002-04-12 Member: 423Members
    <!--QuoteBegin-DOOManiac+Jan 21 2005, 01:09 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (DOOManiac @ Jan 21 2005, 01:09 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Hehe. Sorry, couldn't resist. <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html/emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo--><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    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-->
Sign In or Register to comment.