view 3-Prolog/day1-self-study.pl @ 55:3a20e8b49bc4

Write some self-study code with an additional stretch rule to find collaborations
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Sep 2017 20:26:45 +0100
parents
children 2bbb377ddeb0
line wrap: on
line source

% Make a KB of books and authors and query it.
book(the_long_earth).
book(night_watch).
book(lord_of_the_rings).
book(route_666).
book(krokodil_tears).
author(terry_pratchett).
author(stephen_baxter).
author(jrr_tolkein).
author(jack_yeovil).
wrote(terry_pratchett, the_long_earth).
wrote(stephen_baxter, the_long_earth).
wrote(terry_pratchett, night_watch).
wrote(jrr_tolkein, lord_of_the_rings).
wrote(jack_yeovil, route_666).
wrote(jack_yeovil, krokodil_tears).

collaborated(Author1, Author2) :- \+(Author1 = Author2), author(Author1), author(Author2),
    wrote(Author1, Book), wrote(Author2, Book), book(Book).
% collaborated(terry_pratchett, stephen_baxter). returns true, but collaborated(Author1, Author2).  just says "no", as does
% collaborated(terry_pratchett, OtherAuthor). - for some reason it isn't filling in answers.
%
% If I remove "\+(Author1 = Author2)" then it successfully returns stephen_baxter for terry_pratchett but also returns
% Pratchett himself as a valid answer as well.
%
% wrote(terry_pratchett, Book). works to return all books by an author, though.