Friday, February 28, 2014

What is Recursion

It’s the seventh week of CSC148 and I took the midterm for CSC148 as well as CSC240 and I don't have much time to sit down and think about what is recursion, but since I’ve survived the week, I finally had some time to write down my thoughts on recursion.

Recursion, in my own viewpoint, is to divide a problem into several smaller problems which are easier to solve and the solutions of these smaller problems, when combined together, formed the complete solution to the original problem. Here I take a quite simple example: the Fibonacci sequence (see http://siyangcsc148w3.blogspot.ca/2014/01/some-thoughts-on-recursion.html for detail). Suppose we want to know the nth element in the sequence, then we need to know the (n-1)th and (n-2)th element in the sequence since all elements in the Fibonacci sequence is the sum of two elements that are immediately precede it and to know the two elements, same process will be applied until we reach the first two elements which are defined to both be 1. So the process is actually breaking bigger problems down to smaller problems until a solution is reached and then the solution is passed back and combined to form the complete solution. Here the solution to the smallest problem is called the base case and the process of reaching complete solution is called recursive steps or constructor cases.

But then comes the question: why should we use recursion? I’ll say that the use of recursion could significantly reduce the length and complexity of the program when used properly. (Check my previous post http://siyangcsc148w3.blogspot.ca/2014/01/some-thoughts-on-recursion.html for detail). Like the function we use in assignment1 for moving cheeses among four stools. A recursively defined process only requires 3 lines to reach the goal while it would be quite lengthy to write a procedural function since there are so many cases that one could easily miss something and even one does consider all cases, the code could be quite lengthy and became harder to understand than the 3-line recursive function. However when can we say is a good time for recursion? Certainly, many recursion could be written in for loops like the problem 1 in the extra section of lab 4 – write a recursion function that reverses string s and it could be easily written in for loops as we learned in CSC108, and it only requires 3 lines. So reverse a string might not be a good time for us to use recursion since recursion neither reduces the length of the code nor makes it easier to understand. Nevertheless, there is someplace that recursion could play an important role – when a program requires consideration of many different cases in for loops, recursion could be quite useful as the assignment1 I mentioned earlier. So, when thinking about using recursion, first consider the for-loop version of the program, and if the for-loop version doesn’t require considering too many cases and is not hard to understand, then use for loops instead of recursion. Also, if we want different solutions from the same problem, then recursion is, I’ll say, useless since recursion is based on the assumption that smaller problem only has one unique solution which can be used to construct the solution for bigger problems. One more thing, recursion couldn’t be used on particularly large problems since most programming languages limit the recursion depth to a certain level to avoid crashing of the whole system so when encountering particularly large problems, either use specially designed recursion or find another way without recursion.


In the end, I want to discuss a bit about debugging recursion. Basically a recursion needs a base case and recursive steps that lead to the base case or at least, smaller cases. You can find more on my previous slog http://siyangcsc148w3.blogspot.ca/2014/02/at-end-of-this-slog-httptracycsc148.html.

Monday, February 17, 2014

Debugging recursion in a systematic way

At the end of this slog http://tracycsc148.blogspot.ca/2014/02/csc148-slog-object-oriented-programming.html, the author mentioned “a recursive function will not stop running until the condition in the 'else' part is met” which sounds easy to understand but actually hard to recognize such an error when actually coding. So here I am about to talk about how I debug for recursive functions.
               First of all, the most obvious one – syntax error. It’s obvious doesn’t mean it’s always easy to recognize. Like the preorder function for Trees,

(This is not quite right. Please refer to CSC148 website for the correct version)
When brackets and parenthesis get mixed up, it became much harder to detect where we missed a right parenthesis or a bracket at a glimpse and it’s very likely that we need spend quite a while reading line by line if coding larger projects. Such thing is inevitable but we can always try to avoid it by splitting them into different blocks – instead of writing ternary if, writing in different blocks as we did in CSC108 makes such ‘silly’ error more apparent and may save much time.
               Then comes to the run-time error where under most circumstances problems occur. Most people trying to write recursive function must have experienced error message like ‘Maximum recursion depth exceeded’ which indicated the program falls into an infinite loop. To fix such an error, the very first thing, to me, is to test the base case – an infinite loop always mean the program can’t get to the base case or the base case is in an infinite loop. So it is essential to trace the base case and make sure it’s functioning. For instance of the Fibonacci sequence I wrote in my previous slog (http://siyangcsc148w3.blogspot.ca/2014/01/some-thoughts-on-recursion.html), a functioning base case is essential for keeping the program getting into infinite loop. After the base case, check the constructor cases. Starting from small cases, like the third element in Fibonacci sequence and print out things returned by every recursive step. Still, I will take my fib function as an example.

What I did is to print tuples made up of the fib result and its associated number.
Suppose I type fib(4), it will print (1,2) and (1,1) ,(2,3)and return 2. So I will know fib(2) , fib(1) and fib(3) are all working and if it’s not working, I could quickly find out at which point the recursion failed and fix it.

I used this method in Assignment 1 where Tour module worked for 8,9,10 and 11 cheeses but not for 12 cheeses. As I print out all the recursion steps, I found my initial set up for 2 cheeses is wrong where I set i=0 for 2 cheeses instead of 1. And it saves huge amount of time than tracing by hand or wondering around.

Wednesday, February 5, 2014

Coding style

    This slog http://slogslogslogslog.blogspot.ca/2014/01/week-4-inheritance-exceptions-and.html is quite intriguing to me. It talks mostly about the importance of writing syntactically correct programs, and I believe this plays a huge role in writing programs. Different programming languages have their own syntax so it is essential to learn the correct syntax at the very beginning and then apply the correct syntax. Otherwise, obviously, ‘Syntax Error’ will prevent any program from running and the logic behind it could never be implemented. In addition to syntax, I would like to talk more on coding style. A good coding style is something that makes codes easier to read, easier to follow, and easier to understand. Think about the following example.







    Both function return the power three of x and syntactically correct but the first function is, under most circumstances, prohibited. There are two reasons: First: the function header shows nothing about what the function will do. (Who knows what will ‘dosomething’ do). Second: Writing function body just after the function header does not show the block of the function which makes it harder to read when the function body became longer and longer. So the second function, clearly has a better coding style: clear header, apparent blocks and is taught as a standard way by almost all teachers. So like assignment1, when my group and I start working on that, the very first thing is to set up a consistent coding style, so that everyone could understand what others are writing more efficiently and can add codes without too much modification. Like the initialization of TOAHModel, we set the names of the attributes of the objects as ‘stools, cheeses, etc.’ So when other members of the group and TA wants to read it, it is obviously much more understandable than simply putting ‘a,b,c’ as attributes.
To sum up, a good coding style saves a lot of time and makes both programmer and debugger easier to understand and trace the program, so it is important to have correct syntax but more important to have a good coding style.