Friday, February 6, 2015

My impressions on tracing recursion

On the 4th week of CSC148, many of us getting accustomed to programming were introduced to a technique, called recursion. In simple words, recursion is a programming technique in which a function or method calls upon itself in order to solve a problem. The purpose of this week wasn't necessarily teaching us how to code recursive codes, but more regarding how to actually trace recursive codes and predict the output to a recursive function or method,

On the first lesson, we were introduced to a programming style where you can carry out simple functions with items in a list or dictionary with just one line instead of creating loops and calling out each element in the list or dictionary. For example, let there be a list of integers named I. In order to square every element in the list, instead of calling out a loop, we can use the command [item ** 2 for item in L]. I really liked this method since it looks a lot more shorter and elegant rather than calling out a loop.

On the same day, we started learning how to recursive code. We traced a recursive function called sum_list, which takes in an integer or an arbitrarily nested list of integers, and returns the sum of all integers in it. While doing this exercise, there were two thoughts that came to my mind. The first one was that the same problem could have been solved by using more iterations or loops, but then I realized that it would be a very complicated process and would be very messed up to read when it comes to debugging the program. In that sense, I felt like recursion is a very powerful programming tool since it makes the problem solving process very short, elegant and readable. The second thought that came to my mind was the fact that just for this problem, one didn't have to undergo the rigorous process of recursion since all they had to do was get the sum of all the integers in the list, no matter how they are listed. However, once we started going in detail regarding how to solve it, I realized that I shouldn't rely on this intuition since it might cause errors in other recursive problems. Therefore, I told myself that I would properly follow the proper steps for tracing recursive functions. I also did a bunch of recursive exercises which helped me strengthen my recursion tracing skills.

On the next day, we applied the concept of recursion in order to carry out a sorting process called mergesort sorting. Just like selection or insertion sort, it is a way to sort numbers. The way it works is that it takes in a list of numbers, divides it into two parts. Then it applies the same process to those two parts and continues on until there are several lists which only contain at most 1 integer. Then, it sorts the elements in ascending (or descending, depending on user) order. Since you need to call the same steps over and over again, we decided to go about this using recursion, and it made the process a lot more shorter. It was a very interesting exercise. We also did labs where we further polished our recursion tracing skills.

In order to understand more about how recursion actually works, you can refer to week 5 blog of the link http://csc148slogz-z.tumblr.com/. The person here breaks recursion down and explains how recursion works in a program where we find the depth of a nested list. Furthermore, you can refer to the February 7 post of the link http://christianlessard.blogspot.ca/ where the person talks about why learning to trace code is important. For those of us who feel like giving up at the moment, this could be helpful to you.

Overall, this has been a very interesting week in CSC148 since I got to learn a brand new concept I had never even heard of before. It felt very thrilling to trace recursive codes. I look forward to learn how to actually code them efficiently. And just like recursion, I look forward to learning more programming techniques in this course and in the years to come. Till then my friends, keep coding and stay awesome. Take care.

No comments:

Post a Comment