% File slow_sort.pl

% slow_sort(L,M) produces the ordered list M by ordering L with the 
% "generate and test" technique: It generates all the permutations
% of L and for each of tehm tests whether the solution is ordered. 

% Of course, this technique is not efficient (slow_sort has exponential 
% complexity!) but it is very easy and can be used whenever we are in need of
% writing quickly a solution to a problem and we don't mind (for the moment)
% about efficiency

slow_sort(L,M) :- permutation(L,M), ordered(M).

permutation([],[]).
permutation(L,[X|K]) :- append(L1,[X|L2],L), append(L1,L2,R), permutation(R,K).

ordered([]).
ordered([_]).
ordered([X,Y|L]) :- X<Y, ordered([Y|L]).

% Sample session

/*
?- consult('slow_sort.pl').
slow_sort.pl compiled, 0.00 sec, 0 bytes.

Yes
?- slow_sort([5,3,4,7,9],M).

M = [3, 4, 5, 7, 9] ;

No
?- 
*/
