Thursday, January 30, 2014

Week 4: Recursion and nested list

This week we learnt how to find the depth of nested list in a recursive way. It is the first time for me to learn recursion, it seems much more simple than for loop or while loop, because its results are always very short, maybe in one or two lines. But it is hard to figure out how it runs for me.

Let's look at an example first(which we learnt in class):














All recursion need a base case and a recursion step.

What I think difficult to do is to build the recursion step, it is really hard for me to find the relation ship between the base case and the current case. I was struggled for a long time on e2.

But fortunately, one of my friend taught me that I shouldn't think too much on the whole procedure, just figure out the relationship by simplify the current cases.

I did so, and was surprised to find I am done, I made it. It is no doubt that recursion is not that difficult, just analyse in a right way, every one can grasp it.

2 comments:

  1. ya i guess you must be very good with recursion now!

    ReplyDelete
  2. well... always building from the bottem of recursion. I mean it. linkedlist for example, if you try to use recursion, begin from None, not the listnode which points to None and contains something. You will find some tiny change can simplify your code a lot.

    ReplyDelete