annotate 3-Prolog/day1-self-study.pl @ 62:96e77f914f9b

Remove unnecessary statements
author IBBoard <dev@ibboard.co.uk>
date Fri, 29 Sep 2017 20:02:14 +0100
parents 49cb2f7af872
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
62
96e77f914f9b Remove unnecessary statements
IBBoard <dev@ibboard.co.uk>
parents: 61
diff changeset
24 collaborated(Author1, Author2, Book) :- wrote(Author1, Book), wrote(Author2, Book), Author1 \== Author2.
56
2bbb377ddeb0 Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents: 55
diff changeset
25
2bbb377ddeb0 Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents: 55
diff changeset
26 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
27 % 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
28 % 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
29 % 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
30 %
3a20e8b49bc4 Write some self-study code with an additional stretch rule to find collaborations
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 % 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
32 % 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
33 %
56
2bbb377ddeb0 Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents: 55
diff changeset
34 % 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
35 %
2bbb377ddeb0 Add more examples, more thoughts and a three-argument "collaborated" rule
IBBoard <dev@ibboard.co.uk>
parents: 55
diff changeset
36 % However, having added a three-argument version, it can correctly identify the books that someone collaborated on if
61
49cb2f7af872 Fix the Day 1 collaboration problems
IBBoard <dev@ibboard.co.uk>
parents: 57
diff changeset
37 % you supply both authors but it won't supply an author when given one author and the book title.
49cb2f7af872 Fix the Day 1 collaboration problems
IBBoard <dev@ibboard.co.uk>
parents: 57
diff changeset
38 %
49cb2f7af872 Fix the Day 1 collaboration problems
IBBoard <dev@ibboard.co.uk>
parents: 57
diff changeset
39 % On tracing this and the W&G example then it seems that "\+(…)" doesn't play nicely with variables. The W&G examples
49cb2f7af872 Fix the Day 1 collaboration problems
IBBoard <dev@ibboard.co.uk>
parents: 57
diff changeset
40 % worked because we only asked "are these two people friends" questions, not "who is friends with" questions.
49cb2f7af872 Fix the Day 1 collaboration problems
IBBoard <dev@ibboard.co.uk>
parents: 57
diff changeset
41 %
49cb2f7af872 Fix the Day 1 collaboration problems
IBBoard <dev@ibboard.co.uk>
parents: 57
diff changeset
42 % What we actually need is "\==" (not equal/equivalent). Also, we need to put it at the end so that Author1 and Author2 have
49cb2f7af872 Fix the Day 1 collaboration problems
IBBoard <dev@ibboard.co.uk>
parents: 57
diff changeset
43 % been assigned by that point.