[prev] 39 [next]

Recursive Operations on Lists (cont)

Example: determining the length of a list

// empty list has length 0
// a non-empty list has at least one element
//  plus as many as are in the rest of the list
int length(List L)
{
   if (empty(L))
     return 0;  // base case
   else
     return 1 + length(tail(L));
}

Exercise: write an iterative version of the above