Prolog,家族树怎么说他们是混蛋

时间:2017-03-07 18:59:00

标签: prolog family-tree

例如,如果我有这个祖先,我怎么能说谁是私生子或女儿?

ancestor(frank,mary).
ancestor(frank,andrew).
ancestor(frank,jake).
ancestor(joanne,mary).
ancestor(joanne,andrew).

married(frank,joanne).

法律(还有一些我没有发布的代码,因为它没有相关性)

mother(X,Y) :- ancester(X,Y),gender(X,female).
father(X,Y) :- ancester(X,Y),gender(X,male).

son(X,Y) :- father(Y,X).

sister(X,Y) :- ancester(Z,X),ancester(Z,Y),X\==Y,gender(Y,female).
brother(X,Y) :- ancester(Z,Y),ancester(Z,X), X\==Y,gender(Y,male).

grandfahter(X,Y) :- ancester(X,Z),ancester(Z,Y),gender(X,male).
grandmother(X,Y) :- ancester(X,Z),ancester(Z,Y),gender(X,female).

1 个答案:

答案 0 :(得分:0)

根据评论总结这个问题的答案。

如果他们的父母没有结婚,假设这个人是个混蛋,可以写一个人

bastard(X) :- father(F, X), mother(M, X), \+ married(F, M).

如果您的Prolog方言不支持\+(尽管它符合ISO标准),您应该使用not

bastard(X) :- father(F, X), mother(M, X), not(married(F, M)).