Next: Best and rest
Up: Programming Concepts
Previous: Combine
  Contents
  Index
Subsections
Description
This concept selects the smallest element of a list according to some comparison operator better.
- L
- a list
- V
- a free variable, will be bound to an entry of L
:-mode minimum(+,-).
minimum([H|T],V):-
(foreach(X,T),
fromto(H,In,Out,V) do
minimum_step(X,In,Out)
).
minimum_step(X,Old,X):-
better(X,Old),
!.
minimum_step(X,Old,Old).
This implementation of minimum fails if the input list has no elements. This means that somewhere else in the program we have to handle the case where the input list is empty. This seems to be the most clear definition of minimum, an empty list does not have a smallest element.
If several elements of the list have the same minimal value, only the first one is returned.
Warwick Harvey
2004-08-07