Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 3-Prolog/day2-concat.pl @ 60:643eec1f9e3a
Add a custom implementation of append/3
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 27 Sep 2017 20:48:28 +0100 |
parents | |
children |
rev | line source |
---|---|
60
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 % Rewriting "append" from scratch as "concat" |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 % Append/3 is true when List1+List2 = List3. This can be used to: |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 % * check whether two lists combine to a third (supply all args) |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 % * combine lists (supply first two and a variable for the third) |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 % * remove an item from a list (pass the first and third and the variable in 2) |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 % * computing splits of the list (pass two variables and a list - possibly the least useful, normally) |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 % |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 % The following is written without looking at the code, just the list of features. |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 % Concat an empty list |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 concatenate(List1, List2, List3) :- List2 = [], List3 = List1. |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 % Concat one item from 1 on to 2 |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 concatenate(List1, List2, List3) :- List1 = [Head1], List3 = [Head1|List2]. |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 % Concat more items |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 concatenate(List1, List2, List3) :- List1 = [Head1|Tail], concatenate(Tail, List2, TempTail), List3 = [Head1|TempTail]. |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 % See what we can generalise… |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 % Erm, already seems rather general! |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 % |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 % Looking at the code, we can simplify by: |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 % 1) making the first rule a fact (which I thought I might be able to do, but the wording implied I couldn't. And it worked so I left it) |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 % 2) shifting the unifying from the right to the left |
643eec1f9e3a
Add a custom implementation of append/3
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 % 3) taking advantage of "[Head|Tail]" matching an empty tail to combine the second two rules |