Sunday, February 2, 2014
Recursion
The main focus of this week's lab was on recursion. As a math student, I have had some previous experience with recursions in the context of mathematics when dealing with recursive formulas for sums and integration of trig functions. In general, recursion is a process that repeat its itself until a terminating condition or the base case is reached. (Base case as in the base 'formula/function' that does not require recursion). In the CSC148 lectures, we mostly talk about recursive functions, which are functions in python that calls on itself in its function body. Personally, the key in getting the hang of recursive functions is to carefully trace the examples that Professor Heap has given in class and draw arrows to see at what point of the function we are at. By manually tracing the recursive process, though it may be slow at first, one's mind would start to get used to the repetition and see how it reaches the base case every time a function is ran. Once, it was clear how recursion works, I began to see how it can save lines and lines of code when programming something that requires a repetitive process. However, after lectures when I looked over some of the examples the prof provided during class, these functions can be solved with an iterative solution as well. For example, on the function which checks depth of a list using while loops as well. So I was wondering if there are any preferred methods when one encounters a problem where the iterative method and the recursive method uses the same amount of lines. After some mild research, it can be said that iteration is a type of method that processes a problem repeatedly until it is done while recursion continuously breaks a problem into smaller/simpler pieces until a base case is reached. Thus, when dealing with something that is like a "tree" with a base case in the middle like the turtle example which the Prof has shown, it would be more natural to apply recursion. To conclude, the introduction to recursion in programming has now equipped me with another tool, of which I can apply to solve real life problems, namely, the assignment that is due in two weeks.
Subscribe to:
Posts (Atom)