Monday, March 30, 2009

Recursion makes my brain hurt

Recursion.

It's a simple programming principle: a function repeatedly calls itself until some condition is met. Tree views, such as folder paths, rely on recursion. With a folder tree, you start at the root folder and scan for subfolders. Then you scan through each subfolder and it's subfolders until you've scanned everything to get the entire folder tree. As you can imagine, it's pretty daunting at first.

Another example is cascading menus: you're given a <select> and if you choose a certain option it opens a second menu, which could have an option that opens a third menu and so on. Aaahhh!

I've known about it for many years. Yet it confounds me!

When I try to write something that uses recursion I get so confused. I am able to figure it out, but it's usually a very, very slow process. I wish this came to me more easily. I guess I just need to work with it more to get a better grasp.

I took the author of xkcd.com's challenge ("Which sorting algorithms should I use?") and I was able to write a quicksort, but it took a long time, and I don't know if it's a true quicksort. But whatever it is, it can sort (it works!) and I learned a lot about recursion and sorting in the process.

That's how you learn how to program - by writing programs.

2 comments:

  1. I'm glad to hear you were able to make some progress in the world of algorithms. Frankly, I say you should start an internet poll to ban them. That should surely solve the problem.

    ReplyDelete