Friday, April 3, 2009

Call a function recursively

At the risk of public embarassment, I didn't actually know I could do this:

>>> def recursive(list):
... if len(list) == 1:
... print list[0]
... else:
... recursive([list[0]])
... recursive(list[1:])
...
>>> recursive('hello')
h
e
l
l
o

It seems perfectly obvious now -- it's not like I've never written code like this in other languages, but for some reason I just hadn't been thinking about it.