Sunday, 1 March 2015

Week7: Summary on Recursion

    I've been exposed to a new topic recursion for a few weeks and here is some of my understandings for it.

    Why recursion works?
    Recursion is not a magic and it is not a special grammar in python in my opinion, it is just about using the function over and over again just as the instruction the programmer gave, and at last, use the base case to solve it.

    Are all the functions that contain themselves recursive function?
    No! Sometimes, we may encounter with some __repr__ functions where there are another __repr__ in the function body. However, they are obviously not recursive functions. They are just using this function in other classes. A recursive function should contain a base case, so a good way to judge whether it is a recursive function or not is to find out whether it has a base case.

    How to write a recursive function?
    To write a recursive function, we can trace back. However, it is not so easy to write a recursive function in this method. Rather, we first  think about the base case. We think about the simplest case where no recursion is required to write the base case. And then, we can regard those function calls for simple cases as what we already know and try on cases which just requires one recursion. At last, We may trace back to see whether our function works.

    Above are some of my understands for recursion. Although I get used to recursion to some degree, I still have troubles dealing with recursive functions. I think the way to solve it is to try on more lab problems and ask my friends and TAs(Hope we will have TAs help us soon)


1 comment:

  1. I also had trouble dealing with recursion. They can get really complex at times. The best thing that helped me was to figure out the different cases.

    ReplyDelete