Sunday, February 8, 2015

tracing recursion

Looking back at last week, I had mixed successes and failures in understanding the concept of tracing recursive functions. Recursion is something very different from all the functions I used to see. Its function body calls itself, enfolding many layers. Hence, tracing it is like peeling an onion, I have to peel it layer by layer, and in the end, I just cry.

It is very painful in the beginning trying to follow what the function does for each item in the loop. I had difficulties in figuring out what the function produces, if the function is called into itself again. Luckily, the docstrings and descriptions helped me in understanding what the function is aimed at doing. Many recursive functions we have learned in class involve dealing with nested lists, such as getting the sum of all the integers in the nested lists, or the max value for the values in the nested list. As a practice tracing these functions, I gradually observed that there are always a base case and a loop case. Each time iterating the loop, an item is called into the function again, until the base case is reached and returns a value.
However, towards later this week, I noticed that the docstrings start to disappear. Many recursive functions are presented without the coder describing what it does. This is somewhat challenging. I remember in last week’s recursion tracing quiz, the question was a function that didn’t have description, and I did not figure out an answer. Recursive tracing is still quite difficult for me, consider this example below, I remember I had a lot of trouble trying to deduce what it produces.



I have only figured out what S(1) produces, but starting S(2), and later S(3) and more, I’m confused. I remember I asked my friends about this question, and they appear to be similar to my state. Although I knew that I was not alone, I still wanted to understand it. Eventually one of my friends got the answer, S(2) is ([2 * S(2-1)[0] + 2**1 - 1), which is (3, 1). After he explained it, it starts to make more sense to me. I learned that for recursion, if an example is already traced, the result can be directly substituted into another example, if it contains that portion. I think in the future, I would still need to work on recursive tracing more to even get a better grasp of it.

No comments:

Post a Comment