Thursday, 30 January 2014

week 4 SLOG

  This week I learned a great deal more about recursive functions, and I finally finished my first assignment, which admittedly drove me up the wall when it comes to the part where I have to code not 1, not 2, but 3 recursive functions. This amount of practice, while taxing - both mentally and physically (I couldn't sleep well because I can't stop thinking about it), led me to the realization that some problems are just impractical or plain impossible to code without using recursion. Prior to this week's class, and my work on the assignment, my opinion about recursion has not been favourable. I had always thought that recursive functions are fat, ugly functions that are not worth the effort, but now I
know better. Recursion is simple and elegant, and has the uncanny ability to reduce the length of code significantly. This is not to say that the reduction process is easy though - it is not. So, here I would like to, like last week list down a few short points for my future reference:

  • Recursion should be used when the problem cannot be, or is impractical to be solved using loops alone
  • The trick to coding a recursive function is to figure out the base case(s) - there are more often than not more than one.
  • If it is too difficult to figure out the base case(s), consider writing a more convoluted version of the function, and then reduce it.
  • Induction thinking is generally going to help.
  • Always make sure that the recursion will eventually end. 

No comments:

Post a Comment