Friday, February 28, 2014

Week 7 - Recursion



Recently, there was a popular movie starring some very famous people called Inception. I’m not going to talk about that at all because it’s super obvious and it’s been done to death.

Now, recursion is a little unsettling at first. How can we have a definition that references itself? The Oxford English dictionary would never define “pointless exercise” as “an exercise which has no point”. Our intuition tells us that we need to enlist the help of some outside information to produce a workable definition. Not so with recursion. All we need is a base case, or terminating condition, which prevents us from going too far down the recursive rabbit-hole.

Admittedly, it takes something akin to a leap of faith to write that first self-referencing function. And the first time it works it’s a little bit magical. But with a little bit of tracing it’s not so hard to understand how recursive solutions work, and that recursive solutions are both powerful and elegant. This stems from the fact that many of the problems we face in computer science are inherently composed of similar nested sub-problems that can be reduced to base cases. The tool we used when confronted with these repetitive problems in the past was iteration, which is only a tenable solution when confronted with reasonably shallow nesting and predictable conditions. When the nesting becomes more complex and branching, an iterative solution becomes prohibitively complex, and recursion is much more favourable. In the case of binary trees, for instance, a recursive solution allows us to traverse the data therein without knowing exactly how deep the nesting goes, as iteration would. See What's Gabbening for an exciting and enlightening picture of such a tree.  As long as a recursive function has a correct base case at which it terminates, it could perform operations on an almost infinitely branching tree, with computer power being the only limiting factor.

Recursion has so profoundly changed my worldview that I’m even using it when I don’t have to. For instance in this week’s lab involving linked lists I proudly flaunted my recursive solution, only to find that apparently there was a non-recursive solution that was easier to implement.  I’m still a little indignant. 

Anyway, I’m going to repeat this joke here because probably no one saw it a few weeks ago. I think it’s classic.

What’s Benoit B. Mandelbrot’s middle name?

Benoit B. Mandelbrot.

Friday, February 14, 2014

Week 5



 
Well, fellow Sloggers, I’m sure you need no introduction to the ordeal that was A1. Talk has turned now to the notions of trees and nodes. Although recent lectures have been a bit of a blur, given mainly to the midterm season malaise fogging my brain, one point has stuck: the mantra, “a node cannot have two parents”. If you’re in the Monday/Wednesday morning section at 10, you know what I’m talking about.

Yeah now I’m just stalling.

Anyway, I’ve been enjoying the labs more than I thought I would. This week we learned about two comprehensions, zip and filter, that I wish had been on my radar before now. I can remember numerous times that I had to write such functions longhand, with for loops and if statements and all that. Now it seems like a waste of time. The natural next step was to see what other sort of time-saving (line-saving?) functions already exist in Python for me to abuse and exploit. There is a function called “reversed” that reverses a given iterable. I’m sure that would have been handy at some point. Even more potentially useful is the function “map” which applies a given function to every item of a given iterable. That could have saved me innumerable lines of code over my lengthy programming career. Probably, I don’t 
know, a dozen or two at least.

Right, I started this by talking about trees and nodes. It seems like I’m never going to be able to get away from this recursion stuff. And honestly I’m not sure I’d want to. Looking at these recursive trees blossom is pretty enthralling. Not that I’d admit it to my friends. 

Uhhh....

Monday, February 3, 2014

Week 4



Well a whole week has passed since my last Slog thing. I can barely recognize the person I was back then. I have a feeling that staying regular with these things will require a herculean effort. But I’ll try. I Sure will. Yup.

They say that we’re afraid of that which we don’t understand. And it’s true that, for a long time, exceptions both baffled and terrified me. But this week we pulled back the curtain a bit, and they’re not quite so menacing anymore .After being on the receiving end of their curt disapprobation for so long, it’s gratifying to finally be telling someone else that they messed up. It’s also good to have some sort of idea what the wall of text Python spits out at me means. I imagine in a statistical sense there are more wrong ways to use code than correct ways, so I’m glad exceptions exist to hold anarchy at bay.

Also it’s pretty exciting to finally learn what all this recursion stuff is about. A while ago if you’d asked me, I probably would’ve told you that a function that calls itself was impossible at best, and irresponsible to the stability of the universe at worst. At risk of collapsing space as we know it, I’m starting to see how useful a function with such behaviour could be. It reminds me of a joke I once heard:

What’s Benoit B. Mandelbrot’s middle name?

Benoit B. Mandelbrot.