Reputation: 353
I am trying to list all the cousins of a particular person in my prolog program but can't seem to get it to work. I've checked my code and it seems correct but I am not getting the output I want.
father(john, johnny).
father(john, peter).
father(josh, william).
father(simone, betty).
mother(mary, johnny).
mother(mary, peter).
mother(catherine, william).
mother(kate, betty).
parent(A,B) :- father(A,B).
parent(A,B) :- mother(A,B).
siblings(B,G) :- parent(P,B), parent(P,G), B\=G.
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X\=Y.
I want when I query cousins(X, william).
to return the 2 cousins but I am only getting false as a return. What am I doing wrong?
EDIT: heres what I have now, but can only get 1 cousin to display
father(grandpa1, mary).
father(grandpa1, catherine).
father(grandpa2, john).
father(grandpa2, simone).
father(john, johnny).
father(john, peter).
father(josh, william).
father(simone, betty).
mother(grandma1, mary).
mother(grandma1, catherine).
mother(grandma2, john).
mother(grandma2, simone).
mother(mary, johnny).
mother(mary, peter).
mother(catherine, william).
mother(kate, betty).
parent(A,B) :- father(A,B).
parent(A,B) :- mother(A,B).
siblings(B,G) :- parent(P,B), parent(P,G), B\=G.
cousins(X,Y) :- parent(A,X), parent(B,Y), siblings(A,B), X\=Y.
Upvotes: 2
Views: 3650
Reputation: 60014
Here is a picture of your database
that shows there are no cousins, just because there are no relations among parents. Let's add an unknown, but shared, grandparent:
?- forall(cousins(X,william),writeln(X)).
johnny
peter
betty
If you're interested, the graphical representation can be obtained from this github repo, with this minimal 'glue' code:
parent_child(P,C) :- parent(P,C).
Upvotes: 7