comparison 3-Prolog/day1-self-study.pl @ 61:49cb2f7af872

Fix the Day 1 collaboration problems
author IBBoard <dev@ibboard.co.uk>
date Fri, 29 Sep 2017 20:00:50 +0100
parents c9873b78e8c2
children 96e77f914f9b
comparison
equal deleted inserted replaced
60:643eec1f9e3a 61:49cb2f7af872
19 wrote(ian_stewart, science_of_discworld). 19 wrote(ian_stewart, science_of_discworld).
20 wrote(jrr_tolkein, lord_of_the_rings). 20 wrote(jrr_tolkein, lord_of_the_rings).
21 wrote(jack_yeovil, route_666). 21 wrote(jack_yeovil, route_666).
22 wrote(jack_yeovil, krokodil_tears). 22 wrote(jack_yeovil, krokodil_tears).
23 23
24 collaborated(Author1, Author2, Book) :- \+(Author1 = Author2), author(Author1), author(Author2), 24 collaborated(Author1, Author2, Book) :- author(Author1), author(Author2),
25 wrote(Author1, Book), wrote(Author2, Book), book(Book). 25 wrote(Author1, Book), wrote(Author2, Book), book(Book), Author1 \== Author2.
26 26
27 collaborated(Author1, Author2) :- collaborated(Author1, Author2, Book). 27 collaborated(Author1, Author2) :- collaborated(Author1, Author2, Book).
28 % collaborated(terry_pratchett, stephen_baxter). returns true, but collaborated(Author1, Author2). just says "no", as does 28 % collaborated(terry_pratchett, stephen_baxter). returns true, but collaborated(Author1, Author2). just says "no", as does
29 % collaborated(terry_pratchett, OtherAuthor). - for some reason it isn't filling in answers. It does, however, correctly 29 % collaborated(terry_pratchett, OtherAuthor). - for some reason it isn't filling in answers. It does, however, correctly
30 % say that Pratchett never collaborated with Pratchett. 30 % say that Pratchett never collaborated with Pratchett.
34 % 34 %
35 % wrote(terry_pratchett, Book). works to return all books by an author, though. 35 % wrote(terry_pratchett, Book). works to return all books by an author, though.
36 % 36 %
37 % However, having added a three-argument version, it can correctly identify the books that someone collaborated on if 37 % However, having added a three-argument version, it can correctly identify the books that someone collaborated on if
38 % you supply both authors but it won't supply an author when given one author and the book title. 38 % you supply both authors but it won't supply an author when given one author and the book title.
39 %
40 % On tracing this and the W&G example then it seems that "\+(…)" doesn't play nicely with variables. The W&G examples
41 % worked because we only asked "are these two people friends" questions, not "who is friends with" questions.
42 %
43 % What we actually need is "\==" (not equal/equivalent). Also, we need to put it at the end so that Author1 and Author2 have
44 % been assigned by that point.