Quiz 12 Solution

Recursion

def __getitem__(self, i):
"""Returns the element at index i of the list,
           or throws an IndexError if index is too large."""
    if self._isEmpty():
        raise IndexError('list index out of range')
    elif i == 0:
        return self._head
    else:
        return self._rest[i-1]                      # recurse 

Last modified: Saturday, 01 May 2010