Friday, February 28, 2014

Week 7 - Recursion

Hey there!

Today, I feel like I am a computer scientist! Today, you will read about recursion. Recursion? Again? Really? Yes, I know the recursion sections of some of my posts seem to be just rewrites of the other, but if you didn't notice, for every entry on recursion, I provide more information as I understand it more.

This will likely be the first and last entry going in-depth about this, as I can confidently tell you that I now understand how recursion works (wow, I must be awesome)! Alright, let's begin.

What is Recursion?
    Recursion is when you call a function or a method inside itself (in simple terms). The purpose of this varies, from 'unwrapping' a nested list to perform functions on its elements. It's rather easy to analyse a recursive method or function, but occasionally difficult to come up with your own to solve a problem because it's counter-intuitive. If I told you to create a method that prints the string "spam", n amount of times, where n is some natural number, then you will probably use some sort of loop that involves n as a terminating condition. You will write something similar to the following:

However, this method can be written using recursion:

In the above example, we have completely avoided any loops without ruining the functionality. When you are working with larger-scaled data types (such as nested lists), you can in most cases significantly decrease the number of lines of code when iterating over them by using the above method. The above example is simplistic, so the change in the number of lines between the two examples is negligible.

Problems
    Recursion does have its own set of problems. For example, it does take an ample amount of time to come up with recursive methods for seemingly large functions, and so, developers usually tend to avoid them. With recursion comes the RuntimeError due to incorrectly designed recursive methods. It is raised as a safeguard to prevent a stack overflow, preventing the Python interpreter from tracing a recursive call after a certain amount of times. You can change the amount of times of recursion tracing depth if you would like (the developers of Python assume that we are all smart and kind people), but note that if this safeguard was not there or prevented, then your program may cause a segmentation fault (accessing read-only memory illegally), meaning that your program will crash, and in extreme cases, your computer will too. In order to prevent this problem, I suggest that you create a robust method on paper and test it thoroughly. Make sure you understand how a function works in the first place before you attempt to remake it. Remember, it isn't necessary to make a function recursive. Stick to my motto: "Do not fix what isn't broken - unless the professor says so."

I'm glad that I understand recursion now, and I hope you also have an idea of what it is as well. I'll see you next week!

Bye for now!

No comments:

Post a Comment