问题描述
这有什么区别:
X = Y
还有这段代码:
dif(X, Y)
我认为它们的行为应该相同,但事实并非如此.示例如下:
I thought that they should behave the same, but they do not. Here's the example:
n_puta(L, N, X) :- nputa(L, N, 0, X).
nputa([], N, C, _) :- N = C.
nputa([G|R], N, C, X) :- G = X, nputa(R, N, Y, X), C is Y - 1.
nputa([G|R], N, C, X) :- dif(G,X), nputa(R, N, C, X).
这里有一些调用:
?- n_puta([a,a,b,b,b], 2, X).
X = a ;
false.
?- n_puta([a,a,b,a,b,b], 3, X).
X = a ;
X = b ;
false.
X 应该是在列表 L 中恰好出现 N 次的原子.如果我将 dif(G, X)
替换为 G = X
,我不会't 得到预期的结果.有人能告诉我这两个运营商有什么区别吗?除了 dif(G, X)
之外,我可以使用其他任何东西吗?
X should be the atom that occurs exactly N times in the list L. If I replace dif(G, X)
with G = X
, I don't get the expected result. Can someone tell me what is the difference between these two operators? Can I use anything else except dif(G, X)
?
此示例在 SWI-Prolog 中完美运行,但在 Amzi 中不起作用!序言.
This example works prefectly in SWI-Prolog, but doesn't work in Amzi! Prolog.
推荐答案
dif/2
和 (=)/2
是一样的,只要它们的参数是地面.但只有 dif/2
是一个纯关系,它也适用于变量,并且可以在所有方向上使用.您的示例清楚地表明您应该在这种情况下使用 dif/2
,因为您不仅使用谓词进行测试,还使用谓词生成解决方案.最广泛使用的 Prolog 系统都提供 dif/2
.
dif/2
and (=)/2
are the same as long as their arguments are ground. But only dif/2
is a pure relation that works correctly also with variables and can be used in all directions. Your example clearly shows that you should use dif/2
in this case, because you use your predicate not only to test, but also to generate solutions. The most widely used Prolog systems all provide dif/2
.
这篇关于X=Y 和 dif(X,Y) 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!