Sunday, March 2, 2014

Recursion Returns

More about recursion this week, after a very good midterm.

In all honesty, recursion is probably the biggest thing I will take away out of what we have examined so far in computer science, and the reason is twofold: it's really useful and it is a very regular occurrence in day-to-day life.

Picture this: every time you perform an action to complete the same action again on a different number of things, you are doing recursion. For instance, you have a pile of eight books, and you want a book towards the bottom. Because I'm in a good mood we can do this with a picture.

------ Book A ------
------ Book B ------
------ Book C ------
------ Book D ------
------ Book E ------

We'll start with five and see where that gets us. Let's say the books are really heavy. Like, biology textbook levels of heavy. You can't move a whole bunch at once, but you need that damn Book E.
You could shove the pile over, but most of the time that creates a mess and risks damaging your nice books, so instead you remove Book A and Book B (I realize at this point I am basically recreating Assignment 1, but this proves it actually was a sensible assignment to write).

------ Book C ------
------ Book D ------  ------ Book A ------
------ Book E ------  ------ Book B ------

Now presumably you don't need the books in a particular order so you can put C and D on top of A and B (although if they had different sizes you would). Cool. But we just did recursion, guys. The pile is smaller so that we can do the same thing again for a smaller parameter, in this case books C and D.
That's recursion in your life right now, being all sneaky and recursive. What makes it so useful is that it is a wired-into-your-head feature for completing an action. Doing similar things in multiple steps is often recursive. It's to make things more efficient: that's why in language you can have recursive statements in which an inner part is embedded in a larger one, rather than repeating yourself or making two sentences.

This is what makes computer science recursion really neat, because it's letting our computers be more like our brains (of course, Skynet and all, but live optimistically). And computers aren't quite as good as our brains (yet) so recursion is even more useful for them.

Better example: packaging. There's a fair amount of "packaging"-type stuff in computer science, and when I say "packaging" I mean putting things within things within things, like you've got something in a package in a package.

Let's pretend I am really awesome and built a robot to wrap presents for me because I am the worst at wrapping presents. And because the wrapping robot was lonely I built an unwrapping robot to hang out with it. And let's, just for the sake of the example, pretend they wrap things like so:

Wrapper sees an object 'book'
Wrapper computes how fragile 'book' is and determines the value 2 (out of 5) for a little fragile
Wrapper decides it will repeat its wrapping algorithm a number of times until the book's fragile value is equal to its wrapping value (i.e. it is wrapped enough that there's no danger in it being damaged).
Wrapper begins to run its algorithm on 'book':
Start:
object: 'book'
fragile value: 2
wrap value: 0
fragile value != wrap value
First run-through: (put object in a list)
object: ['book']
fragile value: 2
wrap value: 1 (determined based on number of lists - can be found with recursion!)
fragile value != wrap value
Second run-through: (as before)
object: [['book']]
fragile value: 2
wrap value: 2 (as before)
fragile value == wrap value
object wrapped

Now the 'book' is wrapped through recursive run-throughs, so Unwrapper can unwrap it!
Unwrapper works like Wrapper but reversed, and without a fragile value because Unwrapper gets a little crazy - it just unwraps until the object's wrap value is 0.
Start:
object: [['book']]
wrap value: 2 (again, determined recursively)
wrap value != 0
First run-through: (take object out of one list - you can just re-assign object to object[0])
object: ['book']
wrap value: 1 (as before)
wrap value != 0
Second run-through: (as before)
object: 'book'
wrap value: 0 (as before)
wrap value == 0
object unwrapped

Tadah! The book was on efficiency, which is Unwrapper's favourite subject! What good friends those two robots are.

Thus, a simple example of recursion. You can make a code that sums the number of nested lists; even one which can work for multiple objects in the same wrapping.

But to hammer the point home, this is what makes recursion so neat. It has the power to understand more tasks than, say, a for loop (recursion works for any depth of object - a for loop on its own requires a certain number of specified inner loops). You can even make recursion only go a certain number of times!
For instance, say you have a very deep nested list and you want to go to a certain point and have the computer show you how deep you are. Well you could make a function which takes recursive depth as a parameter, and then reduce the size of that parameter each time you recursively run the function! I.e.

rec_depth([[[[[[[['eight layers deep']]]]]]]], goto=4)
so the code starts by checking the number of lists in and comparing that to goto - it's not deep enough so it starts again
rec_depth([[[[[[['eight layers deep']]]]]]], goto=3)
why is the goto changed? because the list is smaller, so its position is actually further up - this would continue until you get a goto of 0
rec_depth([[[['eight layers deep']]]], goto=0)
at this point, you would stop and do something else. maybe, for instance, the computer prints the new list, or prepends 'you are here' inside (so it prints [[[['you are here', [[[['eight layers deep']]]]]]]]).

Anyway, I think I've gone a bit over the limit here - that's recursion though, in all its glory!

1 comment:

  1. I'll often write recursive code like that myself, passing parameters to control the recursion, but boy, do I feel dirty afterwards. It can make your code really messy with a lot of "hidden" arguments flying back and forth.

    ReplyDelete