Tag Archives: Recursion

It’s Turtles All The Way Down

turtles-793490_1920

Have you ever had only 20 minutes to write a blog post, and you realize you have nothing really to say that particular day, but it’s been a couple of days since you’ve said anything so you just write whatever comes to you? It’s important to check in every once in a while and let people know you’re still out there and to give a gentle reminder and plug for the various books you may have written, even some of the older ones people might have forgotten about but that are totally still worth buying. You can’t tackle anything too ambitious, with a lot of pictures or thought. We’ve all got a couple of blog posts floating around in our heads that we’d love to do if we ever sat down and had an 1.5 hours to format them and make a really good argument, but today isn’t going to be that day.

Then, just when you’ve started writing your twenty minute post, you realize that what you really want to write about is the thought process behind writing a twenty minute post. Maybe you want to get people to try to relate to who you are as a writer at that particular moment, or to offer some tip for people dealing with this situation. Sure it feels a bit meta to be blogging about blogging, but that’s only a couple of layers removed and you might really have something valuable to offer. We all have to figure out how to create quality content on a deadline, and being in the middle of an actual crisis may give you a special insight into how to help others get out of it.

Thinking about how to deal with writing a twenty minute post gets you to thinking about the best ways to give writing advice. Should you only be talking about the things you’re dealing with at a particular moment or should you write more reflective posts on the tips you’ve discovered after years of learning? Writing about what you’re dealing with at the moment can be a good way to choose topics, but it might not be the best way to offer any real insight. After all, you might just be guessing how to get yourself out of a situation without any real idea if that solution would even work. Perhaps you should write a blog post about the best ways and times to give writing advice. So we’re writing a blog post about writing an advice blog post on how to write a blog post in twenty minutes while trying to write the post in twenty minutes.

But we can go one layer deeper. We haven’t even begun to deal with the existential question of why writers write, and what’s the difference between a writer and an author. Are bloggers writers in the same sense as people who write books? If the majority of the writing you actually do is just nonsense falling out of your head without being applied to your current work, can you call yourself a writer? Sure words are magically appearing in front of you as you play the keyboard like a piano, or a well … keyboard, and that might be writing. But is it good?

Oh, I almost forgot. We could wonder if writing about how to give advice to writers is actually art, and whether such writing is considered professional or amateur. It could all be a meta-meta exercise designed to kill time and give the illusion of creating something interesting, when in fact we’ve been up our own butt for some time now.

Ooops … time’s up!

1 Comment

Filed under Writing

Everybody can be a programmer (Recursion)

A couple of months ago, President Obama wrote his first lines of code as part of the “Hour of Code” program for National Computer Science week. You can check out his experience here and take a crack at the hour of code yourself at this site. In that spirit, I thought I’d take today to teach you one of the main concepts of programming, recursion.

Recursive logic is something that almost all programmers struggle with at first, and is a source of continued humor. In the glossaries of many computer science book the definition given for recursion is:

Recursion see recursion.

By the end of this post I hope you’ll not only get the joke, but get the concept as well. This is one of the most powerful, elegant ways to code, and it is simple enough that your five year old has already used it.

Recursive algorithms are made up of two parts, a repeated action (or function) and a base case. In programming one way we do a repeated action is a loop. This can be for a fixed amount of time, or until a certain condition is met.

Fixed

counter = 0
while (counter < 100) {
  do something
  counter = counter + 1
}

Conditional

numberOfHeads = 0
while (numberOfHeads < 50) {
  flip a coin
  if (heads) {
    numberOfHeads = numberOfHeads + 1
  }
}

In this example the fixed loop will run 100 times and stop, the conditional loop will run until heads turns up 50 times. The loop repeats everything (in italics) until the while condition is met.

A few quick definitions before we get started. A function is a set of instructions executed in linear (top-down) fashion. It can call (run) other functions. Those called functions run a set of instructions, then return to the function that called them, continuing execution of the calling function after the line where the other function was called.

Examples help:

function A {
  do this
  then do this
  call function B
  finally do this
  return
}
function B {
  do this some more
  return
}

When function A is run the order of execution would be:

do this
then do this
call function B
do this some more
return
finally do this

Now let’s take our coin flipping example and write it as a recursive algorithm. Again a recursive algorithm has both a repeated action and a base case. The repeated action is flipping the coin, and the base case is when we’ve gotten 50 heads.

function flippingCoins(numberOfHeads) {
  flip a coin
  if(heads) {
    numberOfHeads = numberOfHeads + 1
  }
  if(numberOfHeads = 50) { // Base Case
    stop flipping
  }
  else {
    call flippingCoins(numberOfHeads) // Recursive Call
  }
  return
}

The base case (highlighted in bold) is when we get 50 heads. Instead of looping back to the top we call the function from inside itself. For own coin flip example, this doesn’t seem to make much of a difference and looks like a lot more code for a simple loop. But in other cases, this recursive call has a stacking effect. Instead of repeating the same action over and over (like flipping a coin) each recursive call has an effect on the step above it.

Let’s take a look at another example:

function foldPaper(numberOfFolds) {
  fold paper in half
  numberOfFolds = numberOfFolds + 1
  if(numberOfFolds = 4) {
    make cuts
  }
  else {
    call foldPaper(numberOfFolds)
  }
  Unfold Paper
  return
}

This function takes a sheet of paper, folds it in half four times, makes some cuts, then unfolds the paper. Our base case is after we’ve made four folds, and our recursive call is to fold paper. We’ve added one more step to this function which is to unfold the half of paper we just folded at the end of the function.

So our first step is to start with a full sheet of paper:

IMG_3934

Then fold it in half (adding 1 to the number of folds).

IMG_3935

Since we’ve only made 1 fold our base case is not met so we call our function again. Now we’re not starting with a full sheet of paper, since our first function call folded the paper in half, so when we repeat the fold, the paper is now folded in quarters.

IMG_3936

So far we’ve made 2 folds, so our base case is still not met, so we call the foldPaper function again. As before, the foldPaper function starts by folding the paper in half.

IMG_3937

Now we’ve made three folds, we still haven’t met our base case yet, so we call foldPaper one more time.

IMG_3938

After four folds our paper is folded into sixteenth’s and we’ve met our base case. So now we can make a few cuts.

IMG_3939

After we make the cuts we unfold the paper once and start at the place where the previous function called us.

IMG_3940

After the function call to foldPaper returns we unfold the paper again.

IMG_3943

And again.

IMG_3944

And again.

IMG_3942

Remember, functions execute top to bottom. Here’s what our full function call would look like.

Call foldPaper
  fold paper in half
  Call foldPaper
    fold paper in half
    Call foldPaper
      fold paper in half
      Call foldPaper
        fold paper in half
        cut paper
        unfold paper
        return to foldPaper
      unfold paper
      return to foldPaper
    unfold paper
    return to foldPaper
  unfold paper
end

The cuts were only made once but had an effect based on the number of times the paper was folded. If the paper was folded one less, or one more times, the resulting image would have been different, and each fold had an effect on the first one. And the unfold paper step had to happen for us to be able to see that image, but only after the cuts had been made.

If this had been a loop it would have been like that coin flip, folding a full sheet of paper in half each time. We’d never see the smaller and more intricate patterns.

Hopefully now you’ve got some basic understanding of what recursion can do or at least how it works. If you understand this, then you’re well on your way to becoming a great programmer.

 

2 Comments

Filed under Trube On Tech