Friday, 28 February 2014

Recursion is one of the most useful tools in programming as it allows program to run more efficiently and effectively by calling the function itself over and over again. An example of recursion can be found in the first assignment in this course.

In the 5th step of the first assignment of the course, I am required to implement the move method from TOAHModel class over and over again. The first step is to move n -  i many cheeses from the original stool to one of the intermediate stool. This can be done by creating a recursive function that instruct cheeses from the original stool to be moved to one of the intermediate stools for n - i many times. Then, I used recursion function to move i cheeses from the original stool to the destination stool, again, by using recursion. Lastly, I moved n - i cheeses that are stored in one of the intermediate stool into the destination stool. Because of recursion, this implementation can be expressed in only about 15 lines excluding the headers, doctrings, and comments. However, without the help of recursion, it might take longer lines to execute this implementation.

I find the concept of recursion to be very simple to grasp. I can easily understand what I am required to do and how recursion is needed to get the functions to run. However, I often struggle when I have to write the recursion body. It is difficult because it requires me to think creatively and cleverly to find the corrent code in order for the functions to run. I am still wondering how I can improve my creative thinking skills so that I can quickly write the correct code in recursive functions. At this time, the best solution that I can think of to improve my problem solving skills is to face a lot of recursion problems and try to find the correct code.

Recursion function is useful expecially when programmers face problems that require them to execute the same function over and over again but using different arguments. For example, the nested_depth function from the lecture requires us to repeat the body of the function if the item in the list is a list, and it will require us to use recursion until the item in that list is not a list. Programmers use recursion to instruct the function to do the same thing with different arguments until it meets the expectation of the question. This is why recursion is useful because otherwise, programmers have to keep duplicating the body of the function, which is not very effective and efficient. 

No comments:

Post a Comment