63
|
1 % Skipping Towers of Hanoi - need to think how to map it to rules when it is about movement.
|
|
2 %
|
|
3 % Reversing a list
|
|
4 reverse_list([], []).
|
|
5 reverse_list([Head], Reversed) :- Reversed = [Head].
|
|
6 reverse_list([Head1|Tail], Reversed) :- reverse_list(Tail, ReversedTail), append(ReversedTail,[Head1], Reversed).
|
|
7
|
|
8 % Finding the smallest element
|
|
9 % Remember: Prolog evaluates both sides, so a rule with "Head > Holder" first only happens if
|
|
10 % that statement is true!
|
|
11 find_smallest([], Smallest, Smallest).
|
|
12 find_smallest([Head|Tail], Holder, Smallest) :- Head > Holder, find_smallest(Tail, Holder, Smallest).
|
|
13 find_smallest([Head|Tail], Holder, Smallest) :- Head =< Holder, find_smallest(Tail, Head, Smallest).
|
|
14 % And give the user a sensible version that doesn't need them to supply an arbitrary (potentially wrong)
|
|
15 % initial value
|
|
16 find_smallest([Head|Tail], Smallest) :- find_smallest([Head|Tail], Head, Smallest).
|
|
17
|
|
18 % Sort a list
|
|
19 insert_sort([], Sorted, Sorted).
|
|
20 insert_sort([UHead|UTail], [], Result) :- insert_sort(UTail, [UHead], Result).
|
|
21 insert_sort([UHead|UTail], [SHead|STail], Result) :- UHead > SHead, insert_sort([UHead], STail, NewSTail), insert_sort(UTail, [SHead|NewSTail], Result).
|
|
22 insert_sort([UHead|UTail], [SHead|STail], Result) :- UHead =< SHead, insert_sort(UTail, [UHead|[SHead|STail]], Result).
|
|
23 % Friendly user version
|
|
24 insert_sort([UHead|UTail], Result) :- insert_sort([UHead|UTail], [], Result). |