确定旧的继承顺序序言 [英] determine the old line of succession prolog

查看:42
本文介绍了确定旧的继承顺序序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于英国王室,仅使用male(X),female(X)和parent(X,Y),其中X的父级为Y,如何在prolog中创建Y为X的后继者的继承线(X,Y)

Given the british royal family, Using only male(X),female(X) and parent(X,Y) where the parent of X is Y, how to create the line of succession(X,Y) where Y is the successor of X in prolog

我已经定义了所有的男性、女性和父母

I have defined all the males and female and parent

我也试过这个:

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)).

显然它只在 anne 之前有效在 louise 之后,应该是 anne 而不是 peter...

apparently it only work before anne after louise, it should go anne but not peter...

我已经根据他们在父母(X,Y)中的出生来安排事实.事实:

I have arrange the facts according to their birth in parent(X,Y). Facts:

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)时得到的结果.

Results that i have gotten so far when querying successor(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 ;

在这一点之后,当我尝试去安妮家族时,它出错了.

After this point, when i try to go the Anne family it goes wrong.

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 ;

进入我想要的安妮树时所需的输出是

the desired output when going to anne tree which i want is

    X = elizabeth,
    Y = anne;
    X = anne,
    Y = peter ;
    X = anne,
    Y = savanna ;
    X = anne,
    Y = isla ;

我一直在尝试各种组合,这是迄今为止我得到的最接近的组合X 是父级,而 Y 是 parent(X,Y) 中的子级.

i have been trying all sorts of combinations and this is the closest i got so far X is the parent while Y is the child in parent(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)).

他们都没有工作.

显示旧世系的实际结果是查尔斯家族,安德鲁家族,爱德华家族,最后是安妮家族.

The actual result that show the old line of succession is charles' family, andrew's family , edward's family and lastly anne's family.

推荐答案

你的问题是你使用的条件比需要的复杂,数据库不完整.

Your problem is that you use a condition more complex than needed, and the database is not complete.

具体来说,查尔斯和彼得错过了性别,如此代码所示

Specifically, charles and peter miss gender, as this code show

?- forall(((parent(P,_) ; parent(P,_)), \+(male(P);female(P))), writeln(P)).
charles
charles
peter
peter
charles
charles
peter
peter
true.

儿子和女儿取决于性别,让你条件失败.您可以更正您的数据库,或者坚持使用更简单的方法:

son and daughter depend on gender, and make you condition fail. You could correct your database, or stick to something simpler:

successor(X,Y) :- parent(X,Z), (Y=Z ; successor(Z,Y)).

这篇关于确定旧的继承顺序序言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆