Sunday, 2 February 2014

Recursion Part 1

print("You need to ", recursion())
def recursion():
    return "understand recursion to " + recursion()

Oh recursion, I remember the first time we met, a year ago. Frustration and bewilderment at first sight.
I remember the first time really thinking about you, I really wanted to love you, you promised me so much: shorter code length,
unmatched readability
and of course, the ability to churn out solutions to problems at a blinding rate.
You said you were better than iteration. At first I would have dismissed you as blasphemous, how dare you speak against our savior ITERATION!

In this new year, you have appeared again. I was ready to ignore all your nonsense, but you showed me the light. My hatred of you was just a misunderstanding. All those wasted hours of trying to trace every function call in your recursive depths, that was not the way. This week, the Overlord Python Preacher tried to show us your true beauty. He enlightened us by saying that recursion is the act of breaking down a problem into multiple, simpler, repetitive blocks which we can easily solve. He told us not to try and trace your complex being, but, to rather trust that you will deliver.

So now, here we stand. Recursion is the procedure of breaking down a complex problem into manageable pieces. There is a method which is calls itself over and over again, successively breaking down the large problem till the solution is trivial.

I was taught to trace the recursion, call by call, that worked for the easy examples like factorial, but it became extremely complex on things such as the tower of Hanoi (Ann hoy?) assignment. Now, the way I solve recursive problems is just designing the code for a couple very trivial test cases and trusting it to work for larger problems. This obviously isn't the best way, but it works great for now.

No comments:

Post a Comment