0

我有一个像下面这样的家谱:

Dasarath - Kousalya
         |
       Raam         -        Sita
                    |
                Lava, Kusa

我的事实是:

male("Dasarath").
male("Raam").
male("Lava").
male("Kusa").

female("Kousalya").
female("Sita").

child("Raam", "Dasarath").
child("Raam", "Kousalya").
child("Lava", "Raam").
child("Lava", "Sita").
child("Kusa", "Raam").
child("Kusa", "Sita").

我有一个定义父母的规则。

parent(ParentName, ChildName) :- child(ChildName, ParentName).

还有定义父亲和母亲的规则。

father(FatherName, ChildName) :- parent(FatherName, ChildName), male(FatherName).
mother(MotherName, ChildName) :- parent(MotherName, ChildName), female(MotherName).

现在问题来了,当我将兄弟的规则定义为

brother(BrotherName, PersonName) :- father(ParentName, BrotherName), father(ParentName, PersonName),
                                      male(BrotherName), not BrotherName = PersonName.

我使用查询执行它

brother(BrotherName, PersonName)

结果为

brother(BrotherName, PersonName)
BROTHERNAME = "Lava".
PERSONNAME = "Kusa".

BROTHERNAME = "Kusa".
PERSONNAME = "Lava".

2 Solutions

我被要求只为这个查询带来一个结果。我无法搜索正确的问题。如果有解决方案,请提供一个或告诉我 Prolog 为何如此工作的原因。

4

1 回答 1

2

您可以通过比较它们的名称来省略其中一个结果:

如果两个人都是男性并且有相同的父亲并且第一个的名字大于第二个的名字,那么两个人就是麻烦:

brother(BrotherName, PersonName) :- father(ParentName, BrotherName), father(ParentName, PersonName),
                                      male(BrotherName), BrotherName > PersonName.
于 2015-03-28T08:35:07.317 回答