鉴于英国皇室,仅使用男性(X),女性(X)和父母(X,Y),其中X的父母是Y,如何创建继承线(X,Y),其中Y是继承人序言中的 X
我已经定义了所有的男性和女性以及父母
我也试过这个:
son(X,Y) :- parent(X,Y), male(Y).
daughter(X,Y) :- parent(X,Y), female(Y).
successor(X,Y):- (son(X,Z);daughter(X,Z)) , (Y=Z;successor(Z,Y)).
显然它只在路易斯之后的安妮之前工作,它应该去安妮而不是彼得......
我已经根据他们在父母(X,Y)中的出生安排了事实。事实:
male(charles).
male(william).
male(peter).
male(henry).
male(andrew).
male(edward).
male(viscount).
male(savanna).
female(elizabeth).
female(anne).
female(zara).
female(beatrice).
female(eugenie).
female(louise).
female(isla).
parent(elizabeth,charles).
parent(elizabeth,anne).
parent(elizabeth,andrew).
parent(elizabeth,edward).
parent(anne,peter).
parent(anne,zara).
parent(charles,william).
parent(charles,henry).
parent(andrew,beatrice).
parent(andrew,eugenie).
parent(edward,louise).
parent(edward,viscount).
parent(peter,savanna).
parent(peter,isla).
我在查询后继者(X,Y)时得到的结果。
X = elizabeth,
Y = charles ;
X = elizabeth,
Y = william ;
X = elizabeth,
Y = henry ;
X = elizabeth,
Y = andrew ;
X = elizabeth,
Y = beatrice ;
X = elizabeth,
Y = eugenie ;
X = elizabeth,
Y = edward ;
X = elizabeth,
Y = viscount ;
X = elizabeth,
Y = louise ;
在此之后,当我尝试去安妮家时,它出错了。
X = anne,
Y = peter ;
X = anne,
Y = savanna ;
X = anne,
Y = isla ;
X = charles,
Y = william ;
X = charles,
Y = henry ;
X = edward,
Y = viscount ;
X = peter,
Y = savanna ;
X = elizabeth,
Y = anne ;
X = elizabeth,
Y = peter ;
X = elizabeth,
Y = savanna ;
X = elizabeth,
Y = isla ;
X = elizabeth,
Y = zara ;
X = anne,
Y = zara ;
X = andrew,
Y = beatrice ;
X = andrew,
Y = eugenie ;
X = edward,
Y = louise ;
X = peter,
Y = isla ;
去我想要的安妮树时所需的输出是
X = elizabeth,
Y = anne;
X = anne,
Y = peter ;
X = anne,
Y = savanna ;
X = anne,
Y = isla ;
我一直在尝试各种组合,这是迄今为止我得到的最接近的 X 是父母,而 Y 是父母(X,Y)中的孩子。
尝试过的后继组合:
successor(X,Y):- (son(X,Z);daughter(X,Z)) , (Y=Z;successor(Z,Y)).
successor(X,Y):- parent(X,Z), (Y=Z ; successor(Z,Y)).
successor(X,Y):- (male(Z);female(Z)) , (Y=Z;successor(Z,Y)).
successor(X,Y):- (parent(X,Z),(male(Z);female(Z)), (Y=Z;successor(Z,Y)).
他们都没有工作。
显示老继承人的实际结果是查尔斯家族,安德鲁家族,爱德华家族,最后是安妮家族。