-3

我需要序言中的代码。

假设我们在家谱中定义了一些人。

如果他们具有同一代并且谁是他们的同一个祖先,我该如何编写一个函数来获取两个人的姓名和进程?

parent(chester,irvin).
parent(chester,clarence).
parent(chester,mildred).
parent(irvin,ron).
parent(irvin,ken).
parent(clarence,shirley).
parent(clarence,sharon).
parent(clarence,charlie).
parent(mildred,mary).


male(chester).
female(mildred).
male(irvin).
female(shirley).
male(clarence).
female(sharon).
male(ron).
female(mary).
male(ken).
male(charlie).



father(X,Y) :- parent(X,Y), male(X).

mother(X,Y) :- parent(X,Y), female(X).

grandparent(X,Y) :- parent(X,Z), parent(Z,Y).

paternalgrandfather(X,Y) :- father(X,Z), father(Z,Y).

sibling(X,Y) :- parent(Z,X), parent(Z,Y).

brothers(X,Y) :- sibling(X,Y),male(X),male(Y), \+ (X=Y).

samegeneration(x,y) :- HERE I DONT KNOW WHAT TO DO
4

1 回答 1

0

Break it down: start with a predicate for two people having a common ancestor, where having the same parent would be your base case. Then enhance that to keep track or how many generations from that common ancestor each person is. One more step (which is left as an exercise), and you're done!

于 2012-01-05T16:48:05.650 回答