本文介绍了X\=Y 和 dif(X,Y) 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有什么区别:

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) 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-29 03:46