Friday, February 28, 2014

Recursion: An In-depth Look

When faced with a large and repetitive problem in the real world, it’s highly impractical to keep repeating the tedious little details over and over. Thus, we often find ways to break the problem up into little parts that come together to complete the task. The same approach can be applied to computer science, resulting in recursion.

Recursion can be defined as a method in which a bigger problem can be solved by solving smaller and smaller instances of the same problem, which come together to solve the initial problem. A simple example of recursion would be returning the sum of values in a nested list. We’d keep summing non-list elements, and then call the function again on a list element inside the initial list. Each inner list would then return a sum once a base case is reached, and that is key to recursive functions. It keeps recursing and going deeper until a specified base case is reached (acting as a signal that the element in question is finally acceptable to work with), at which point the function finally returns a value (in this case, the sum). I personally like to compare the workings of recursive functions to the Russian Matryoshkya dolls, because like the dolls, recursive functions keep going deeper within themselves to solve a problem.

One important thing to keep in mind is when and when not to use recursion. Generally, after using it for a while in class and with a bit of reading, I realized that recursion is extremely useful as compared to iteration when working/sorting through nested data. Otherwise, when working with linear data, recursion can just overcomplicate things, and therefore we should stick with iterative solutions. Ultimately, the goal of computer scientists should be to use whichever approach solves the problem in the most efficient and clear way. This is presumably what the instructors want us to realize because by introducing recursion to us, they want us to be equipped with the tools required to tackle various different types of problems.

In conclusion, recursion was difficult for me in the start because I had never attempted to do anything like it before. However, after a few weeks of tracing and hands-on work in the labs and A1, I feel like I’ve become comfortable with it and can use it whenever it is required. 

No comments:

Post a Comment