Sunday, 8 February 2015

Recursion-ursion-sion-n

      For the past couple of weeks we have been learning about a concept that I'm sure is crucial to writing code in computer science; this concept is recursion. As far as I understand, recursion allows a function to call itself. In a recursive function, there is a case that causes the function to recur (call itself), and a base case (when the function produces a result without it recurring). The base case is important because it breaks the recursion/terminates it. I believe this would prevent infinite recursion, and thus stop the program from crashing. We have been tracing through some simple recursive functions, and I notice some differences between recursion and iteration in a loop. Recursion looks a lot more concise, and generally the code looks nicer and more professional. It also looks like recursion is useful when you want to work with sublists, because recursion allows you to access those lists without having to write out complex code, simply by calling the function within itself on each sublist. I find some of the recursive calls confusing. For example, the ones that contain 1 + max([some function(x) for x in L]). I was initially confused about the order of application of max and +1, but I think that I understand it now. I'm going to practice more examples. Tracing recursive code also becomes difficult when there are many sublists, because it's like doing a complex math problem: you have to know your order of operations. One way that recursion is different from other code tracing that we've gone though, stems from the fact that once you've evaluated a sublist or done a recursive call on it, stop tracing it. This is a difficult concept to get used to, since I am used to things being carried over.
      Even though recursion seems like this brilliant, fabulous new concept, I can't help but question the efficacy of its applications sometimes. Surely there must be some downsides to recursion? In the stack, which we are now learning about as well, space is allocated to a function and all of its local variables and arguments (I think) when the program starts. What happens then, if too much recursion is happening inside your function, and the set amount of space that was originally allocated runs out? The program would crash! And as far as I can tell, the program would run more slowly than if you were to implement loop iteration. There must be some way to prevent the stack from overflowing, but I don't believe  python is a language that has any optimizations built in. I sat in on one of my friend's programming languages classes. Dr. Racket was being used, and it had something called tail call optimization built in, in order to prevent the stack from overflowing. I just wanted to connect that idea, because I think it's interesting to apply it to what we're learning! So, with more practice and lessons about recursion, I hope to become better at using it, and learning about more of its applications.

No comments:

Post a Comment