0

大家好,我是序言中的新手,我有这样一个列表:(实际上它是我的谓词的输出而不是列表)

P = [1/1, 1/3] ;
P = [1/1, 2/3] ;
P = [1/3, 1/1] ;
P = [1/3, 2/1] ;
P = [2/1, 1/3] ;
P = [2/1, 2/3] ;
P = [2/3, 1/1] ;
P = [2/3, 2/1] ;

我需要删除重复的术语。例如 [1/1,2/3] 和 [2/3,1/1] 是相同的,我应该删除其中一个,哪个不重要,我该怎么做在序言中?提前致谢

注意我了解到 findALL 应该是解决这个问题的好方法,但仍然不知道答案,请帮助我。

4

1 回答 1

0

Unless you actually show us your code, it's never going to be possible to give you precise answers.

I assume you have a predicate f/1 such that:

?- f(P).

produces the interactive result you show above. A simple solution is to change your query:

?- f([X,Y]), X < Y.

This will produce the following result:

X = 1/3, Y = 1/1 ;
X = 1/3, Y = 2/1 ;
X = 2/3, Y = 1/1 ;
X = 2/3, Y = 2/1 ;

findall/3 isn't sufficient to solve this particular situation, because you've defined uniqueness in a way that ignores the position in the list. In Prolog (and everything else) [X,Y] and [Y,X] are not equal, so you'd have to find a trick to get this to give you "unique" results.

于 2013-06-11T20:45:10.977 回答