[prev] 45 [next]

"Map" functions on lists

The previous example is a special case of
  • a function that traverses a list
  • applying a function to each item
The same pattern applies, regardless of function

void applyToList(List L, Function F)
{
   if (empty(L)) 
      /* nothing to do here? */
   else {
     head(L) = F(head(L));
     applyToList(tail(L), F);
   }
}