Recursive Dancing

Friday night, class was over and we, the students, were celebrating the end of a successful week by playing games and dancing. I was dancing to my heart’s content when I looked at the white board. It had the following code:


def fibo_finder(num)
  if num < 2
      num
  else
    fibo_finder(num-1) + fibo_finder(num-2)
  end
end

It was the solution a ruby problem we had earlier in that day. It required us to find the nth number of the fibonacci sequence.

On the board was a solution to the problem created by another student, but it was strange to me. The person created a method called fibo_finder which was calling itself in the block. What? Waat??? I stopped dancing and stared at the board trying to understand what was going on. It was then that Oliver, one of my classmate and the author of whiteboard code, explained to me that what I was staring at was an example of recursion, which is a powerful and elegant tool that allows a method to call itself to reach an end goal. To me it felt like a magical if not awkward loop. It was beautiful.

I thanked Oliver for the explanation and went back to dancing. But the idea of recursion stayed with me. While dancing I thought of my dance steps as a method which called itself. And there I continued repeating the same dance steps over and over again trying to understand how recursion works till I could not remember how many recursive steps I had done and similarly to an old computer, I felt overworked and crashed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s