static int smallest(List L)
{
// The smallest member of a singleton list [x] is x.
if(isEmpty(tail(L)))
{
return head(L);
}
// The smallest member of a list [x,y,z,w] is
// the minimun of x and the smallest of [y,z,w].
else
{
return min(head(L), smallest(tail(L)));
}
}
Here is a looping definition. It keeps track of the smallest value seen so far as it scans the list.
static int smallest(List L)
{
int smallestSoFar = head(L);
for(rest = tail(L); !isEmptyList(rest); rest = tail(rest))
{
int h = head(rest);
if(h < smallestSoFar)
{
smallestSoFar = h;
}
}
return smallestSoFar;
}