annotate 3-Prolog/day2-self-study.pl @ 70:4198fa4e0df4

Add Scala code and notes
author IBBoard <dev@ibboard.co.uk>
date Sat, 14 Oct 2017 13:39:41 +0100
parents f6fce6a54e94
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
63
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 % Skipping Towers of Hanoi - need to think how to map it to rules when it is about movement.
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 %
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 % Reversing a list
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 reverse_list([], []).
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 reverse_list([Head], Reversed) :- Reversed = [Head].
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 reverse_list([Head1|Tail], Reversed) :- reverse_list(Tail, ReversedTail), append(ReversedTail,[Head1], Reversed).
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 % Finding the smallest element
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 % Remember: Prolog evaluates both sides, so a rule with "Head > Holder" first only happens if
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 % that statement is true!
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 find_smallest([], Smallest, Smallest).
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 find_smallest([Head|Tail], Holder, Smallest) :- Head > Holder, find_smallest(Tail, Holder, Smallest).
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 find_smallest([Head|Tail], Holder, Smallest) :- Head =< Holder, find_smallest(Tail, Head, Smallest).
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 % And give the user a sensible version that doesn't need them to supply an arbitrary (potentially wrong)
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 % initial value
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 find_smallest([Head|Tail], Smallest) :- find_smallest([Head|Tail], Head, Smallest).
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 % Sort a list
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 insert_sort([], Sorted, Sorted).
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 insert_sort([UHead|UTail], [], Result) :- insert_sort(UTail, [UHead], Result).
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 insert_sort([UHead|UTail], [SHead|STail], Result) :- UHead > SHead, insert_sort([UHead], STail, NewSTail), insert_sort(UTail, [SHead|NewSTail], Result).
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 insert_sort([UHead|UTail], [SHead|STail], Result) :- UHead =< SHead, insert_sort(UTail, [UHead|[SHead|STail]], Result).
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 % Friendly user version
e159ec90a072 Day 2 self-study, with insert sort
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 insert_sort([UHead|UTail], Result) :- insert_sort([UHead|UTail], [], Result).