Tuesday, February 10, 2015

Tracing recursions

We learned about recursion last week which was really confusing at the beginning when I saw the codes with it. Basically it's working like a for or a while loop but unlike a ''for'' or a ''while'',  it divides the object into the smallest unit and implement a built-in function on it. Our professor said that it might be confusing when we saw it but after tracing the recursion we would find it magical and that was exactly how I felt after that lecture.

we were given an example where the method takes a list or a single number as the parameter and the code below called the method itself again(a recursion) because we didn't know the exact form of the parameter, which could be a nested list with any depth or just a number. It was hard to set the criteria for the loops in that case so recursion was useful. we started with the basic case where the parameter was a number and that gave us the value returned under the first 'if' requirement. As we proceeded I noticed that a nested list with any depth would eventually be dissected into the basic case where the object is a number and then we got the result using recursion even if the details of the parameter was unknown.

In conclusion, tracing recursions is a great way for us to understand how python works with recursions and that was definitely a clear and enlightening lecture.

No comments:

Post a Comment