Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Prolog &引用;或;运算符只返回第一条语句_Prolog - Fatal编程技术网

Prolog &引用;或;运算符只返回第一条语句

Prolog &引用;或;运算符只返回第一条语句,prolog,Prolog,我有一个典型的关系例子 m(thomas). m(leon). w(nina). born(thomas, nina, leon). born(Father, Mother, Child) :- born(Mother, Father, Child). father(Father, Child) :- born(Father, Mother, Child), m(Father). mother(Mother, Child) :- born(Father, Mother, Child), w(M

我有一个典型的关系例子

m(thomas).
m(leon).
w(nina).
born(thomas, nina, leon).

born(Father, Mother, Child) :- born(Mother, Father, Child).
father(Father, Child) :- born(Father, Mother, Child), m(Father).
mother(Mother, Child) :- born(Father, Mother, Child), w(Mother).
parent(Parent, Child) :- (father(Parent, Child); mother(Parent, Child)).
当我向父母问好时,只有父亲会被列出:

?- parent(X, leon).
X = thomas ;
X = thomas ;
.....
我希望父母双方:

?- parent(X, leon).
X = thomas ;
X = nina ;
我能做什么

编辑:问题:

我的问题不是
,因为它通常会返回这两个语句

问题是上面代码中的
born/3
是一个循环。 我试着让母亲和父亲能够互相交流

编辑:修复:

我从ChristianF和Capelical那里得到了两个答案。他们都帮助我理解了我的陈述的错误之处

我自己的解决方案:

m(thomas).
m(leon).
w(nina).
b(thomas, nina, leon).

born(Father, Mother, Child)
    :- (m(Father), w(Mother), b(Father, Mother, Child)); b(Mother, Father, Child).
father(Father, Child) :- born(Father,_, Child), m(Father).
mother(Mother, Child) :- born(_, Mother, Child), w(Mother).
parent(Parent, Child) :- (father(Parent, Child); mother(Parent, Child)).

你的规则中有一个“循环”。删除born/3规则,您将获得

?- parent(P,leon).
P = thomas ;
P = nina.
编辑,因为你需要一个父母可以留在第一或第二个“位置”,无论性别,我建议

father(Father, Child) :-
   m(Father), (born(Father, _, Child) ; born(_, Father, Child)).

我在born/3查询之前移动了性别测试,似乎可以更有效地避免重复查询…

问题是,由于存在无限递归,有无数种方法可以为父亲显示
born

m(thomas).                                                                  
m(leon).                                                                    
w(nina).                                                                    

born(thomas, nina, leon).                                                   

born_sym(Father, Mother, Child) :- born(Father, Mother, Child).             
born_sym(Father, Mother, Child) :- born(Mother, Father, Child).             

father(Father, Child) :- born_sym(Father, Mother, Child), m(Father).        
mother(Mother, Child) :- born_sym(Father, Mother, Child), w(Mother).        
parent(Parent, Child) :- (father(Parent, Child); mother(Parent, Child)).    

另一个修复方法是使用
cut
实际上是为了避免无限递归:

m(thomas).
m(leon).
w(nina).

born(thomas, nina, leon) :- !.
born(Father, Mother, Child) :- born(Mother, Father, Child).

father(Father, Child) :- born(Father, Mother, Child), m(Father).
mother(Mother, Child) :- born(Father, Mother, Child), w(Mother).
parent(Parent, Child) :- father(Parent, Child); mother(Parent, Child).

这只是修复无限递归的另一种方式,可能不是一种好的方式:)

born(父亲、母亲、孩子):-born(母亲、父亲、孩子)。
是一个很大的麻烦。一件有帮助的事情是为
born/3
事实建立一个惯例,其中父亲或母亲总是排在第一位。你真的应该总是在你的问题中包含你得到的结果,并解释为什么它不是你想要的。我肯定它在“帮助中心”(即FAQ)的某个地方。您已经包含了解释,但没有包含输出本身。@mbrach:这是一个解决方案,但我无法做到。我需要父亲和母亲能够改变彼此。@WillNess:?你甚至没有读最后一句话。在你回答之前,你应该阅读所有的东西!我肯定它在“帮助中心”(即FAQ)的某个地方。缺少的关键信息是你尝试的成绩单的逐字副本。最好包括原始证据;即使是完整的口头描述也是第二好的选择——当你不完全理解某件事时(因为你问了它),很可能你不知道它是什么,当你描述它时,可以安全地忽略它。:)这是正确的。我意识到有一个回路,但找不到它。谢谢但我需要父母彼此原谅。所以我需要这个规则。谢谢。我刚从ChristianF那里选择了解决方案,但我认为你的解决方案将在另一个问题上帮助我。:)没错。但我该如何解决这个问题呢。出生的人不知道或者应该知道?哦,对不起。我没看到身体里没有符号。谢谢你:)我解决了我的问题。