Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 3-Prolog/day1-self-study.pl @ 56:2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 26 Sep 2017 20:45:26 +0100 |
parents | 3a20e8b49bc4 |
children | c9873b78e8c2 |
rev | line source |
---|---|
55
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 % Make a KB of books and authors and query it. |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 book(the_long_earth). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 book(night_watch). |
56
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
4 book(science_of_discworld). |
55
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 book(lord_of_the_rings). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 book(route_666). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 book(krokodil_tears). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 author(terry_pratchett). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 author(stephen_baxter). |
56
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
10 author(jack_cohen). |
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
11 author(ian_stewart). |
55
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 author(jrr_tolkein). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 author(jack_yeovil). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 wrote(terry_pratchett, the_long_earth). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 wrote(stephen_baxter, the_long_earth). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 wrote(terry_pratchett, night_watch). |
56
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
17 wrote(terry_pratchett, science_of_discworld). |
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
18 wrote(jack_cohen, science_of_discworld). |
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
19 wrote(ian_stewart, science_of_discworld). |
55
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 wrote(jrr_tolkein, lord_of_the_rings). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 wrote(jack_yeovil, route_666). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 wrote(jack_yeovil, krokodil_tears). |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 |
56
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
24 collaborated(Author1, Author2, Book) :- \+(Author1 = Author2), author(Author1), author(Author2), |
55
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 wrote(Author1, Book), wrote(Author2, Book), book(Book). |
56
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
26 |
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
27 collaborated(Author1, Author2) :- collaborated(Author1, Author2, Book). |
55
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 % collaborated(terry_pratchett, stephen_baxter). returns true, but collaborated(Author1, Author2). just says "no", as does |
56
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
29 % collaborated(terry_pratchett, OtherAuthor). - for some reason it isn't filling in answers. It does, however, correctly |
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
30 % say that Pratchett never collaborated with Pratchett. |
55
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 % |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 % If I remove "\+(Author1 = Author2)" then it successfully returns stephen_baxter for terry_pratchett but also returns |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 % Pratchett himself as a valid answer as well. |
3a20e8b49bc4
Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 % |
56
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
35 % wrote(terry_pratchett, Book). works to return all books by an author, though. |
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
36 % |
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
37 % However, having added a three-argument version, it can correctly identify the books that someone collaborated on if |
2bbb377ddeb0
Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents:
55
diff
changeset
|
38 % you supply both authors. |