我尝试按照这个递增顺序将[1,2,3,3,4,5,5,6,6,7]
之类的数字列表排序到另外两个列表Unique
和NotUnique
,这只是为了找出一个数字是否出现多次列表。 1
将进入Unique
列表,3,3
将进入NotUnique
列表。
我已根据建议进行了更改,但我在排序列表的末尾保留了_G1678
和_G1708
,这些是什么?
?- removeUni([1,2,3,3,4,5,5,5,6],A,B).
A = [1, 2, 4|_G1678],
B = [3, 3, 5, 5, 5, 6, 6|_G1708].
以下是我的removeUni
代码:
removeUni([],[],[]).
removeUni([X],[X],[]).
removeUni([X1,X2],[X1,X2|Uni],D):-
X1 < X2,
!.
removeUni([X1,X2],Uni,[X1,X2|D]):-
X1 =:= X2,
!.
removeUni([X],[X|Uni],D).
removeUni([X1,X2,X3|Q],[X1,X2|Uni],D):-
X1 < X2,
X2 < X3,
removeUni([X3|Q],Uni,D),
!.
removeUni([X1,X2,X3|Q],[X1|Uni],D):-
X1 < X2,
X2 == X3,
removeUni([X2,X3|Q],Uni,D),
!.
removeUni([X1,X2,X3|Q],Uni,[X1|D]):-
X1 == X2,
X2 == X3,
removeUni([X2,X3|Q],Uni,D),
!.
removeUni([X1,X2,X3|Q],Uni,[X1,X2|D]):-
X1 == X2,
X2 < X3,
removeUni([X3|Q],Uni,D),
!.