%%% SORTING %%% Stupid sort: sort1(L1,L2) :- permutation1(L1,L2),sorted(L2),!. permutation1([],[]). permutation1(L,[H|T]) :- append(V,[H|U],L), append(V,U,W), permutation1(W,T). sorted([]). sorted([X]). sorted([X,Y|L]) :- order(X,Y), sorted([Y|L]). order(X,Y) :- X =< Y. order2(X,Y) :- X @=< Y. /***** Example run: | ?- permutation1([1,2,3],L). L = [1,2,3] ? ; L = [1,3,2] ? ; L = [2,1,3] ? ; L = [2,3,1] ? ; L = [3,1,2] ? ; L = [3,2,1] ? ; *****/ %%%% For more sorting algorithms, see Section 7.7.