Thursday, February 27, 2014

Recursion

       Recursion is a very powerful tool we can access in Python. Actually, most of programming languages support recursion, which shows the importance of recursion in the field of computer science programming. In real life, a lot of problems is much easier after we break them into pieces, then solve those subproblems one by one. Recursion can help us to do that when we code.
       In general, Recursion means "defining something in terms of itself", and in Python, we call the function again inside of that function. However, in order for a recursive function process properly, it has to have at least one base case and at least one general case.
       A general case is where we call the function itself using recursion. Sometimes, we may need to update the input for general cases every time it recurs according to the function requirement. A recursive function may need to call itself a lot of time in order to achieve the optimal goal, but it surely would make the problem a lot easier than solving the problem as a whole. On the other hand, a base case is a restriction to this recursive function. It is defined to stop recursion when it is necessary. Otherwise, the function will call itself until our computer run out of memories or raise an error in some situations.
       An simple example for recursion would be nested_list_max, which finds and returns the maximum integer in a nested list:
                     def nested_list_max(L: list) -> int:
                            """Return the maximum in nested list L.
                            """
                            return max([nested_list_max(i) if isinstance(i, list) else i for i in L])
This function will form a list using list comprehension: it goes into every item in list L, if the that item is a integer, it will add it into the list; if the item is a list, the function will call itself again on that item. After the function goes through every item in list L, it will find the maximum number in the list that made by list comprehension. No matter what is the depth of the list L, the function will always go thought every element in it.
       In the above problem, it looks pretty simple. But if we want to code it without recursion, it would be really hard, since we need to consider the depth of the input list L. Once again, it is really important to have a base case and a general case. In the example, my base case is that if item i is not a list, the list comprehension would add i into the new list. And my general case is that if i is a list, the list comprehension would add the result of nested_list_max into the new list.

      Recursion can do a lot more than the example I showed, and there is still a lot of things I need to learn about it. Once I can ace recursion, I believe coding would be a lot easier in the future.

2 comments:

  1. :) Please feel free to recursively read it, and leave comments...

    ReplyDelete
  2. Interesting perspective of recursion. I gained insight on a few things I've missed. I would read again! 10/10

    ReplyDelete