Sunday, 2 March 2014

A Recursive Post About Recursion

    In addition, the idea of recursion, while not fully understood, has become a lot clearer to me since I first learned it. The idea that, while defining a function that you can call the function perplexed me. How is something like that possible? Wouldn't that be like just calling the function infinity times? It turns out most of my confusion was that... Wait a minute. Does this sound familiar to you as well? That's probably because it is! Welcome to my recursive post about recursion! The first time I did this was a long time ago, in a galaxy far, far away. And since that time, I have learned so much more about recursion. From the myriad of functions we've programmed together, to classes we've created, all based on recursion, there's one thing I've learned; recursion isn't that complicated.

    It turns out that the thing about recursion that confused me the most was the fact that it was written in list comprehension form. As I have mentioned in previous blog posts, I took CSC108 a LONG time ago(about 1 year to be precise) and didn't learn a thing about list comprehensions. What makes it worse is that I missed the entire first week of class because I was stuck in an airport because of storms happening in Canada, so I am still not sure if we learned it then. However, during a lab, the topic of list comprehensions came up, and low and behold, recursion seems to make sense to me now!

    Recursion itself isn't particularly useful. It's just iterating over a function to keep it going without having to embed a bunch of loops around it. It's two things that I think make recursion useful; the built-in python function, whether it be append, or sum, or max, and the condition that the recursion is occurring against. The reason I neglected to mention a base case is because it's not what makes a recursive function useful; it's what makes a recursive function function (recursively). The built-in function is what provides some sort of work done to the function, whether it sorts things from nested lists, or it just creates a new list of things in a different order, this is what lets your function do what it needs to. The recursive condition, on the other hand, is what lets the know where to go, and prevent it from being an infinite loop. Whether it is a for loop, iterating over the items in a list, or a while loop, letting the function occur over a range of numbers, this is easily the most important part of a recursive function, excluding the recursive function itself. By splitting the function into parts, and understanding each portion, it made it much easier for me to understand.

    However, while I understand how a recursive function works, it still confuses me in its uses. For example, things such as linked lists and trees, which are essentially whole classes based on recursion, aren't clear to me. I understand how the code works, that isn't the problem, I just don't understand where and why I would use them. Why is it better than a nested list? Why do we have a specific instance of a tree, and call it a different thing, even though it is just a tree? It's questions like these that keep me up at night (not really though). Well, that's all I have to say about recursion. See you next week!

P.S. I know I haven't done a blog entry for week 6 yet... mostly because I completely forgot about it. I promise it'll be up by this Tuesday!

No comments:

Post a Comment