Saturday, 1 February 2014

Week4: Recursion

During this week, we were taught about recursion. Recursive functions are important and efficient in python to solve complex problems. Understanding its concept, structure and usage is significantly important.


First of all, the difficulty of understanding recursive functions comes from its operation; a function calls itself during the function. We have to trace it until it reaches the base case to figure out how the entire function is been supported. If the function just run infinitely, it produces error. Thus, a recursive function must have its base case, which sets up how and when a function stop its recursive calls. The other part of the function is called general case. 

    Example:
                  def nested_depth(l: list) -> int:
                        return (1 + max([nested_depth(x) for x in L] + [0]) if isinstance(L, list) else 0)

The base case is "if isinstance(L, list) else 0", it makes the recursive function have an end point. No matter how deep the lists are, when it reaches the list that contains no inner list, the result of this recursive call will be 0. Next, it will exit and continue to execute the previous call, and once the previous call finished, it will back to the call before it, and finally the result is returned. 

    Trace nested_depth([14, 7, [5, [3, 17, 1], [2, 4], 6], 9]):

    (1). [0, 0, [0, 1, 1, 0, 0], 0, 0]
    (2). [0, 0, 2, 0, 0]
    (3). 3


Obviously, the body part "1 + max([nested_depth(x) for x in L] + [0]" is the general case. With a step by step analysis of a recursive function, the structure becomes clearer. "Recursive" is very different from "circular" and the key difference is recursion have base cases,  and we try to avoid becoming circular because it is meaningless. I found recursion is similar to a while loop to some extent, which both have a point to stop and run repeatedly. However, loops are not recursive.

Furthermore, recursive functions draw concrete images which are some beautiful fractals. That is an amazing work. In the given notes, I traced the function "koch".  


    Example:

def koch(t, order, size):
    """
       Make turtle t draw a Koch fractal of 'order' and 'size'.
       Leave the turtle facing the same direction.
    """

    if order == 0:          # The base case is just a straight line
        t.forward(size)
    else:
        koch(t, order-1, size/3)   # Go 1/3 of the way
        t.left(60)
        koch(t, order-1, size/3)
        t.right(120)
        koch(t, order-1, size/3)
        t.left(60)
        koch(t, order-1, size/3)
   
    Trace: koch(t, 2, 3)

    - koch( t, 1, 1)
    - t.forward(1)
    - t.left(60)
    - koch(t, 1, 1)
    - t.forward(60)
    - t.right(120)
    - koch(t, 1, 1)
    - t.forward(1)
    - t.left(60)
    - t.forward(1)

As we have seen, the function structure does not change much. To write a recursive function, first of all, we need to think about the base case, the most basic move, and then we write the general case to call the function recursively.

In the end, with the practices on labs and exercises, I become familiar with writing test cases and raising errors. Assignment 1 especially need lots of work on recursion and requires deeper understanding. Throughout the learning, programming turns out to be much more fun!!

2 comments:

  1. good to know that other parts of recursion are called general case. i was wondering how to describe those conditions :)

    ReplyDelete
  2. just one tiny problem is if we input size= any negative number at the first place, the recursion dead..

    ReplyDelete