Sunday 1 March 2015

Week 7

I am choosing to talk about recursion this week, as I consider it a major point in my programming skills to date.
As I mentioned in an earlier post, tracing recursion by hand is a huge help! This helps me realize what the correct output should be, and helps me to check if the code that I write myself behaves as intended. The idea of recursion is to apply a function within itself, so as to compute complex statements that consist of smaller pieces that eventually trickle down to simple solutions. I found a great summary here: http://interactivepython.org/courselib/static/pythonds/Recursion/recursionsimple.html.
Recently, we have been implementing our own recursive code, particularly with trees. This always takes me some thinking - I guess I have not yet gotten fully accustomed to the idea of going inside the same function repeatedly! I find that it helps to draw out a tree and consider what must occur at each step in order to determine the ultimate output. In a way, this is like a pre-tracing of the recursion. It can be clearly seen that recursion is required, and if stated neatly, can be written into the appropriate recursive code without further trouble. The trick is to state the command as though speaking to some root's children in a tree - then, the message will be passed on until the leaves, which will consequently pass up a certain message. At each stage, some other action besides just asking the children may be performed (for example, add 1). The practice that we get in class and especially during lab is extremely helpful for mastering recursion.
It is quite incredible just how useful recursion is. It reminds me of a while loop, but for the entire function rather than for a small segment within a function. Before learning it, I never even considered how I might use it. Now that I have seen it so often, I cannot imagine how I could have written code before!

1 comment:

  1. Recursion's cool: construct base cases, think about how they can be combined when dealing with a case that isn't a base case and voila!

    ReplyDelete