2013年10月19日星期六

csc148_slog_recursion

    In week 4 and week 5 of CSC148, we learned something about recursion and recursive data type. It is a challenging topic for me at the beginning that I need a lot of time to figure out how recursive functions work.
    Recursion is really useful to computer scientists because sometimes we can solve the problems easily by using the function itself.
    Tower of Hanoi is a typical example. How to solve the Tower of Hanoi with 5 cheeses? It's just move the first 4 cheeses to the intermediate stool, then the fifth one to the destination stool, then the first 4 cheeses to the destination stool. We don't care how to move 4 cheeses because as long as we indicate how to move 1 cheese to the destination stool, the program will run recursively to the bottom case: move 1 cheese.
    I think the key to writing recursive function is:
        1.Find what we should do for the bottom case.
        2.Find the relationship between 'n case' and 'n-1 case' (return what? pass what argument to the 'n-1 case'?)
   The difficulty of witing recursive function is to work out a recursive algorithm, but once we work it out, the implemention will be much more easier.

没有评论:

发表评论