% a set of predicates developed from T. Van Le's 'TEchniques of Prolog Programming'
% Wiley 1992
% an alternative find_all
% Usually you have 'findall(Vars,SatisfyingGoal,PutInList)' but if not use:
:-dynamic(found/1).
find_all(X,G,L):-
asserta(found([])),
G,
once(retract(found(L))),
asserta(found([X; L])),
fail.
find_all(_,_,L):-retract(found(L)).
%This is simpler than Schnupp and Bernhard, but is it faster?
% This splits a list up at successive points into a head and a tail
% remove comments if not builtin!
% select(H, [H|T], T).
% select(X, [H|T], [H|T1]):-select(X,T,T1).
% A selection sort from page 40
selection_sort([],[]).
selection_sort(L,[H|T]):- least(H,L,R), selection_sort(R,T).
least(X,[X],[]).
least(X,[H; T],R):-least(Y,R,S),
( H =< Y, (X,Y)=(H,T)
; H > Y, (X,R)=(Y,[H|S])
).