One of the first novel ideas you learn in computer programming is the concept of recursion: functions that call themselves. It is a new idea to most and extremely powerful in the world of computers. It is also the point in the programming class where you figure out if you “get it” or if you should just stop now.
So let me explain the basic idea to those who don’t think in terms of functions or methods, or even know what they are. It is all about dividing a problem into a set of smaller problems. Imagine you are at the end of the line for Splash Mountain at Disneyland and they come tell you they need everyone’s phone number to give them a free ticket on a Disney cruise. But you don’t know everyone’s phone number! In fact, you can’t even see to the front of the line. How do you solve the problem?
You break it up into a smaller problem of course. You know your phone number right? And you know how to tell the person in front of you to do the same (smaller problem because that person has one less phone number (yours) they are responsible for). And if you do that enough times, pretty soon you are at the front of the line. Easy.
That was an extremely simple real world recursion example. They get harder and less tangible. The classic first example you encounter in programming is math factorials. As in, 7! = 7 x 6 x 5 x 4 x 3 x 2 x 1. This is perfectly suited for recursion because it can be easily broken into smaller problems. What is 7 factorial? Well it is 7 times 6 factorial. And what is 6 factorial? It is 6 times 5 factorial. And so on down the line. All you need to make sure is that you have a “base case” to stop on (so that you don’t keep calling the same thing to infinity and go into negative numbers), in this example 1! = 1.
However, there is much more to this classic example than meets the eye. Most programmers will stop there, but as it turns out, there are a ton of ways to actually solve this problem. I came across a ranting post on all the different things you might consider when coding your solution. Things like how big a number it supports, how much computer resources you want to consume, and how easily to switch your approach. It is an interesting foray into the mind of an experienced programmer – if you have ever coded up a way to calculate factorials check out the article.
Photo: fdecomite
Very nerdy skinner. I like the article about all those methods of doing factorial.
The fact that you like the article makes you very nerdy as well. I like that.
google search “recursion”. get’s me every time
HA! I didn’t know about that one. Also check out the search result for “once in a blue moon.”
I know this is nerdy (which I guess is fine for a post about recursion) but I saw this on Cyanide and Happiness and it totally made me think of this post. Imagine recursion creeping into your life via a fortune cookie. Prepare to be trapped in the world of your own circular imagination.
http://www.explosm.net/comics/1605/
I don’t know whether that is better or worse than the fortune cookies that aren’t actual fortunes. Yes, I’m talking about the statement cookie: http://travelogue.travelvice.com/peru/i-hate-statement-cookies/
Good question. I would say that upon first look the recursion cookie would seem worse. Even scary. But then, as you consider the fantastic reasons why you might encounter such a cookie, it would become wonderful. Give you purpose and a sense that some benevolent being was looking out for you.
However, a statement cookie…well…let me just say, “Some men dream of fortunes, others dream of cookies.”
Are you there person behind the statement cookies? You are way too good at it for this to be a coincidence!