在 prolog 调用中比较两个解决方案
我编写序言程序如下。现在,当我将 halfsister 谓词运行为 halfsister(X,Y) 时。我得到了结果
X = ann
Y = sylvia ? ;
X = sylvia
Y = ann ? ;
,但两者都是相同的结果,但显示了两次。如何让程序只显示相同的结果一次? 我的序言代码:
parent(john,ann).
parent(jim,john).
parent(jim,keith).
parent(mary,ann).
parent(mary,sylvia).
parent(brian,sylvia).
male(keith).
male(jim).
male(brian).
female(sylvia).
female(ann).
female(marry).
female(john).
brother(X,Y):-parent(Z,X),parent(Z,Y),male(X),X\==Y.
uncle(X,Y):-brother(X,Z),parent(Z,Y).
halfsister(X,Y):-
parent(A,X),
parent(A,Y),
parent(B,X),
parent(C,Y),
\+(A = B),
\+(A = C),
\+(B = C),
female(Y).
感谢您宝贵的时间。
I write prolog program as below. Now, when I run halfsister predicate as halfsister(X,Y)
. I got result
X = ann
Y = sylvia ? ;
X = sylvia
Y = ann ? ;
but both are the same result but showing twice. How can I make the program to show the same result for once only?
my prolog code:
parent(john,ann).
parent(jim,john).
parent(jim,keith).
parent(mary,ann).
parent(mary,sylvia).
parent(brian,sylvia).
male(keith).
male(jim).
male(brian).
female(sylvia).
female(ann).
female(marry).
female(john).
brother(X,Y):-parent(Z,X),parent(Z,Y),male(X),X\==Y.
uncle(X,Y):-brother(X,Z),parent(Z,Y).
halfsister(X,Y):-
parent(A,X),
parent(A,Y),
parent(B,X),
parent(C,Y),
\+(A = B),
\+(A = C),
\+(B = C),
female(Y).
Thanks for your valuable, time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为没有必要消除这种“重复”,因为谓词
halfsister(X,Y)
的意思是“Y 是 X 的同父异母姐妹”。您有两个答案,一个表示“西尔维娅是安的同父异母的姐妹”,另一个表示“安是西尔维娅同父异母的姐妹”。所以这里没有任何问题。如果你坚持要消除它们,你可以尝试:
查询:
<前><代码>?-同父异母的姐妹(X,Y)。
X = 安,
Y=西尔维娅;
错误的。
I don't think it is necessary to eliminate this "duplication", because the predicate
halfsister(X,Y)
means "Y is X's half sister". You got two answers, one means "sylvia is ann's half-sister", the other means "ann is sylvia's half-sister". So there is nothing wrong here.If you insist on eliminating them, you can try:
Query: