recursion and linked lists

Ed Singleton singletoned at gmail.com
Fri Apr 7 04:37:03 EDT 2006


On 01/04/06, John Salerno <johnjsal at nospamgmail.com> wrote:
> I V wrote:
>
>
> > Note that print gets called after _each_ time that printBackward
> > returns. So, as the different calls to printBackward return, they print
> > whatever 'head' was set to in that invocation. Now, logically enough,
> > the last call to printBackward is the first to return, so the last
> > value of 'head' is printed first, and so in in the reverse order of the
> > list.
>
> Oh, that helps! Now I'm starting to understand when exactly it returns
> each time.

The way I got my head round this was to think of namespaces (I'm not
sure how true this is though).

I think of the first head variable as being:

printBackward.head

When it calls printBackward from within the first printBackward, thye
second variable is:

printBackward.printBackward.head

and so on.  It helps to keep it clear that they are entirely different.

Ed



More information about the Python-list mailing list