Sorting lists: 1. Slow Sort % slow_sort(L, S): Sorts list L, returning S. % Keep permuting L until...
-
Upload
isabel-simpson -
Category
Documents
-
view
213 -
download
1
Transcript of Sorting lists: 1. Slow Sort % slow_sort(L, S): Sorts list L, returning S. % Keep permuting L until...
Sorting lists: 1. Slow Sort% slow_sort(L, S): Sorts list L, returning S.
% Keep permuting L until it becomes sorted.
slowsort(L, S) :-
permutation(L, S),
inorder(S).
permutation([ ], [ ]).
permutation(L, [H|T]) :-
append(V, [H|U], L),
append(V, U, W),
permutation(W, T).
1COSC 2P93 : More examples
inorder( []).inorder([_]).inorder([A,B|T]) :- A < B, inorder([B|T]).
2. Bubble sort
bubblesort(L, S) :-
append(X, [A,B|Y], L),
B < A,
append(X, [B,A|Y], L2),
bubblesort(L2, S).
bubblesort(L, L).
2COSC 2P93 : More examples
3. Insertion sort
insort([ ], [ ]).
insort([X|L], M) :-
insort(L, N),
insortx(X, N, M).
insortx(X, [ ], [X]).
insortx(X, [A|L], [A|M]) :-
A < X,
insortx(X, L, M).
insortx(X, [A|L], [X,A|L]) :-
A >= X.
3COSC 2P93 : More examples
4. Quick sort
qsort([], []).
qsort([H|T], S) :-
split(H, T, A, B),
qsort(A, AS),
qsort(B, BS),
append(AS, [H|BS], S).
4COSC 2P93 : More examples
Quick sort (cont)
% split(H, L, Less, Greater):
% All items in L that are less in value than H are put in Less (in order).
% The rest are put in Greater (in order).
split(_, [], [], []).
split(H, [A|X], [A|Y], Z) :-
A < H,
split(H, X, Y, Z).
split(H, [A|X], Y, [A|Z]) :-
A >= H,
split(H, X, Y, Z).
5COSC 2P93 : More examples
Lotto 6/49 simulator
% Lotto 6/49 generator: generate 6 random numbers (no duplicates!)
?- use_module(library(random)). % include random number library (Sicstus)
lotto649(Numbers) :-
make_intlist(1, 49, All), % see previous overheads!
select_random(6, All, Numbers).
% select_random(N, A, L): selects N numbers from A, putting them in L
select_random(N, _, [ ]) :- N =< 0.
select_random(N, All, [Number | Rest]) :-
N > 0,
size(All, Size), % see “count” from assignment
generate_random_int(1, Size, R),
remove_nth(R, All, Number, Leftover),
M is N - 1,
select_random(M, Leftover, Rest).
6COSC 2P93 : More examples
Lotto 6/49 (cont)
% generate random value between Low and High, incl.
% random/1 returns random float between [0.0, 1.0). (ie. never 1.0)
generate_random_int(Low, High, R) :-
Range is High - Low + 1,
random(X), % Sicstus builtin
R is integer(X * Range) + Low.
% remove_nth(R, All, Item, Rest) removes the Rth entry Item from All,
% leaving Rest. R is between 1 and the size of All.
remove_nth(1, [Item|Rest], Item, Rest).
remove_nth(N, [X|All], Item, [X|Rest]) :-
M is N - 1,
remove_nth(M, All, Item, Rest).
7COSC 2P93 : More examples
Another Lotto 6/49 program
This version accepts an integer seed to scramble random numbers (could do this in previous program as well!)
?- use_module(library(random)).
lotto649(Seed, Numbers) :-
make_intlist(1, 49, All),
setrand(Seed),
random_permutation(All, [A,B,C,D,E,F| _ ]), % Sicstus built-in
sort([A,B,C,D,E,F], Numbers).
8COSC 2P93 : More examples
Integer puzzle solver problem: given a list of integers between 1 and N, generate lists of unique
integers from this list that add up to a given integer value
% int_puzzle(H, N, L) solves the problem in L for integers ranging from 1 to H, adding to value N
int_puzzle(High, Number, List) :-
make_intlist(1, High, All),
find_soln(Number, All, List).
find_soln(0, _, [ ]).
find_soln(Tot, All, [Num|List]) :-
Tot > 0,
remove(Num, All, Rest),
NewTot is Tot-Num,
find_soln(NewTot, Rest, List).
9COSC 2P93 : More examples
int_puzzle(cont)
% remove(N, L, R) removes item N from list L, resulting in R.
% Will fail if N is not found in L.
remove(N, [N|L], L).
remove(N, [A|R], [A|L]) :- remove(N, R, L).
One problem with int_puzzle: returns permutations of solutions Later, will modify to keep track of unique solutions, and filter
duplicated ones
10COSC 2P93 : More examples