首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为prolog创建兄弟谓词?

如何为prolog创建兄弟谓词?
EN

Stack Overflow用户
提问于 2016-03-31 16:40:57
回答 1查看 4K关注 0票数 0

我被告知要创建一个兄弟谓词,以确定一个兄弟是否有兄弟姐妹。Brother(B, S) :-。我知道你需要弄清楚他们是否有相同的父母,但我不知道该如何做。

代码语言:javascript
复制
father(dan,phil).
father(dan,jack).
father(dan,christine).
father(phil,kenton).
father(phil,shula).
father(phil,david).
father(phil,elizabeth).
father(jack,lillian).
father(jack,tony).
father(jack,jenny).
father(tony,tommy).
father(tony,helen).
father(tony,john).
father(roger,adam).
father(roger,debbie).
father(brian,kate).
father(david,pip).
father(david,josh).
father(mark,daniel).

mother(doris,phil).
mother(doris,jack).
mother(doris,christine).
mother(jill,kenton).
mother(jill,shula).
mother(jill,david).
mother(jill,elizabeth).
mother(peggy,lillian).
mother(peggy,tony).
mother(peggy,jenny).
mother(pat,tommy).
mother(pat,helen).
mother(pat,john).
mother(jenny,adam).
mother(jenny,debbie).
mother(jenny,kate).
mother(ruth,pip).
mother(ruth,josh).
mother(shula,daniel).

male(dan).
male(phil).
male(jack).
male(kenton).
male(david).
male(tony).
male(brian).
male(roger).
male(mark).
male(john).
male(tommy).
male(adam).
male(daniel).
male(josh).

female(doris).
female(jill).
female(peggy).
female(christine).
female(shula).
female(ruth).
female(elizabeth).
female(lillian).
female(pat).
female(jenny).
female(helen).
female(debbie).
female(kate).
female(pip).

dead(dan).
dead(doris).
dead(jack).
dead(mark).
dead(john).
dead(fred).


parent(X,Y) :-
        father(X,Y); mother(X,Y).

grandfather(X,Y) :-
        father(X,Z), mother(Z,Y).
grandfather(X,Y) :-
        father(X,Z), father(Z,Y).

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

archer(dan).
archer(X) :- father(P,X), archer(P).

这是我所拥有的定义父母、祖父等的文档。我需要创建一个兄弟谓词,作为兄弟(B是兄弟,S是兄弟)。例如兄弟(利亚姆,佐治亚州)。利亚姆是乔治亚的兄弟,应该是真的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-31 17:34:45

您可以通过首先提供brother(B,S)的正式定义来解决您的问题:

滑动male/1 的兄弟B是一种,它具有 mother/2 M

现在我们已经知道了这一点,我们为每个条件编写了一行或多行。

第一个简单地说是该条款的标题:

代码语言:javascript
复制
brother(B,S) :-

既然兄弟是male/1,我们首先要检查的条件是:

代码语言:javascript
复制
    male(B),

接下来,我们必须检查它们是否具有相同的father/2。因此,我们定义了父亲F,而F必须是B的父亲,因此我们编写:

代码语言:javascript
复制
    father(F,B),

由于这位父亲也必须是S的父亲,我们写道:

代码语言:javascript
复制
    father(F,S),

mother也是如此:我们定义了一个母亲M,并检查这个母亲是否是BS的母亲

代码语言:javascript
复制
    mother(M,B),
    mother(M,S).

现在,我们把这一切结合起来,得到:

代码语言:javascript
复制
brother(B,S) :-
    male(B),
    father(F,B),
    father(F,S),
    mother(M,B),
    mother(M,S).

现在的问题是,使用这个谓词,一个男性将成为他自己的兄弟。如果您不想要这种行为,我们添加一个约束:

一个人并不是他自己的brother/2

因此,您必须使用不相等的谓词:

代码语言:javascript
复制
    B \= S.

或完整谓词:

代码语言:javascript
复制
brother(B,S) :-
    male(B),
    father(F,B),
    father(F,S),
    mother(M,B),
    mother(M,S),
    B \= S.

此谓词生成以下答案:

代码语言:javascript
复制
?- brother(B,S).
B = phil,
S = jack ;
B = phil,
S = christine ;
B = jack,
S = phil ;
B = jack,
S = christine ;
B = kenton,
S = shula ;
B = kenton,
S = david ;
B = kenton,
S = elizabeth ;
B = david,
S = kenton
...
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36339119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档