Prolog - pairs that are not related
parent(kresimir, jasna).
parent(kresimir, darko).
parent(vesna, darko).
parent(darko, vigor).
parent(darko, goran).
parent(vigor, ruzica).
parent(vigor, snjezana).
parent(mario, nenad).
ancestor(X, Y) :-
parent(X, Y).
ancestor(X, Y) :-
parent(X, Z),
ancestor(Z, Y).
related(X, Y) :-
parent(X,Y);
parent(Y,X).
related(X, Y) :-
X \= Y,
ancestor(Z, X),
ancestor(Z, Y).
This is my prolog file, what i need is to find开发者_运维问答 all pairs of people that are not related, and i have no idea how.
You need a way get all the people first.
person(X) :- parent(X, _).
person(X) :- parent(_, X).
Then use setof/3
to get a list of people without duplicates:
setof(X, person(X), People)
and collect all pairs from that:
pair_of_people(X,Y) :-
setof(P, person(P), People),
member(X, People),
member(Y, People),
X \= Y.
This, by the way, produces "mirror" pairs: both (X,Y)
and (Y,X)
for all X
and Y
. You can do better with an adapted version of member
that many Prologs support. Check out your Prolog manual.
Finally, filter out the pairs that are related
:
unrelated(X,Y) :-
pair_of_people(X,Y),
not(related(X,Y)).
精彩评论