Recursion Introduction Introduction

It's finally the weekend. Five grueling days of having to copy down notes and contribute in group projects (which, let's face it, is actually you just doing the entire thing and writing your classmates' names in the title slide) are finally over, giving you a much-deserved break.

Now if only you had something to occupy your time.

Hmmm.

You've already played through all your songs on Dance Dance Revolution (with all the difficulty bars on, double time, like a boss), read every DDR novelization (even the choose your own adventure, Left, Right, Up, or Down?), and watched every Youtube video of people competing on dance machines at arcades (you'd know, you get an alert every time a new video gets added). There's only so much DDR one Shmooper can do.

Just when you feel like you're going to collapse out of boredom, your friend Taylor texts you to ask if you want to see the new mise en abyme exhibit at the SMOMA (Shmoopville Museum of Modern Art, obvi). Even though you've never heard of this mise en abyme, anything's better than sitting around at home, right?

Right.

You grab your wallet and run out the door to catch Taylor and figure out her fixation with French-words-turned-art-terms.

Taylor's waiting for you outside, wearing a dress with a woman holding up a box of cocoa mix. Taylor has a thing for wearing other people's faces on her clothes (you once saw her wear a Langston Hughes shirt with a Justice Sonia Sotamayor purse and Justine Timberlake flip-flops), but this particular dress feels different. When you get closer, you notice that the box of cocoa mix that woman's holding has the exact same woman on it, holding a box of cocoa mix…with the same woman again.

Whoa.

Taylor explains that the pattern, a Dutch ad for cocoa powder, inspired an entire art style called the Droste Effect. It's kind-of trippy, but you can dig it.

Everything clicks when you hit the museum and notice that every picture looks just like Taylor's dress. Not in the Andy Warhol style of painting the same image in four different color sets style, but the idea. Mise en abyme, or 'set in the abyss' in French, is pretty much the same thing. You start with one picture and then place a smaller version of that picture somewhere in the same image. In the smaller image of the same thing and keep going down, getting smaller and smaller, until you can't paint or draw that picture smaller anymore. It's terrifying. Also really cool.

Also: totally recursive. Recursion's all about finding the solution to a problem by first breaking it down into a smaller version of the same problem. If filling the canvas is your problem, you can fill it with a big image, but that might only cover 60% of the canvas. Then you put a smaller version inside that image, you're also covering 60% of that 60%, which…still doesn't fill the entire canvas.

Theoretically, you could go on forever, but after a certain point you just can't make your paintbrush small enough or your hand steady enough to keep filling the space. In recursion terms, that means you've hit the base case.

Art and computer science. They have a lot in common. Who knew?

Besides digital artists, of course.

 

Why Should I Care?

Recursion is as fundamental to computer science as

  • cheesy Western music is to Firefly 
  • meaningful stares are to The Office.
  • the Rodgers and Hammerstein repertoire is to suburban dinner theaters.

For such a simple concept—applying the same function over and over again until reaching a stopping point—recursion is one of the most important tools in a programmer's toolbox. With a few lines of recursion, complex problems melt away, saving time, money, and hassle.

Assuming you don't have to debug, of course.

The best part? No matter your input size, your code won't change. It's so powerful that it'll be able to cut any size of problem into a few simple repeating patterns. Fractals are the perfect example: that giant image just keeps repeating itself in smaller and smaller versions until filling up the whole space.

If you think recursion's only a computer science thing, let's set the record straight. It's vital to mathematics, too. You can't go far in math without hitting a recursively-defined set, proof, graph or graphic (that's, uh…a fractal), or function.

Still, not all problems should get a recursive solution. Recursion's infinite(-ish) power is best used carefully—and in small doses. Too much can slow down your computer or crash your program. It's your job as a computer scientist to know when and how to let your recursive flag fly.

Or something like that.

(Source)