
% smallest(L,M)
% -------------
% gives smallest integer value in list L

% we'll assume 2^31-1 is the biggest int we can deal with
maxint(Max) :- M is 2^31, N is M-1, N = Max.

% use an accumulator, keeping track of the smallest int seen so far,
%     and initially use our maxint value as the smallest so far
smallest(L,M) :- is_list(L), maxint(Max), smallest(L, Max, M).

% smallInt(L, SoFar, Result)
% --------------------------

% if we're at the end then the answer is the smallest we saw previously
smallest([],Sofar,Sofar).

% if H is a number smaller than S then recurse using H
smallest([H|T], S, X) :- number(H), H < S, smallest(T, H, X).

% otherwise recurse using S
smallest([_|T], S, X) :- smallest(T, S, X).

