Sunday, March 1, 2015

Recursion

   Recursion is a tricky but really fun part for me in the study of computer science so far. I am fascinated by the idea of repeating itself within itself, though this idea is also a little complicated to understand and apply. Recursion is very convinient in programming since it simplifies and divides the problems into parts and solves them part by part. It saves lots of unecessary codes and is able to solve extremely complicated problems by several simple codes.
   During the lectures we learn and understand recursion by tracing it, from the simplest base cases to the more complicated ones. The first recursive method we encountered is sum_list. We first take a look at an extremely easy example call such as sum_list(27), which the argument is an int rather than a list. So that we can simply figure out the condition for recursion. Then we can clearly see how it works for a list, such as sum_list([1, 2]) and a nested list, such as sum_list([1, [2, 3]]), and so on.
   With the understanding of recursive methods, now we are able to implement them and apply them! Though the first implementation of a recursive method takes me about an hour to solve it, applying recursion to some methods will become easier after some practice. In last week's lecture, we've tried to implement BYNode.evaluate() method using recursion. I find that it is easier to solve the problem by thinking about the base case. After solving the base case, consider a typical example that needs the idea of recursion.(Agreed by another SLOGer) This is the most difficult part since it is the main part of the function that requires lots of thinking and deep understanding of the method. When both 2 parts are done, we can write them in the 'if...else' form.(We can also check whether the code is correct by tracing them.)

No comments:

Post a Comment