I have been trying to split a given list into two different lists: Unique and Duplicate.
For example, if we have the list [1, 1, 2, 3, 3, 4, 5]
I want the Unique list to be [2, 4, 5]
and Duplicate to be [1, 3]
.
I don't want all the 1's in the list to be in the Duplicate list. I just need one of it.
The code I have right now:
compareL([_|[]], Unique, Dup).
compareL([X3,Y3 | Tail], [X3 | Unique], Dup) :-
X3 =\= Y3,
compareL([Y3 | Tail], Unique, Dup).
compareL([X3,Y3 | Tail], Unique, [X3 | Dup]) :-
X3 = Y3,
skipDups(X3, Tail, Unique, Dup).
skipDups(_, [], Unique, Dup).
skipDups(X3,[Y3 | Tail], Unique, Dup) :-
X3 =\= Y3,
compareL([Y3 | Tail], Unique, Dup).
skipDups(X3,[Y3 | Tail], Unique, Dup) :-
X3 = Y3,
skipDups(X3, Tail, Unique, Dup).
Using the example list given above if I run compareL([1, 1, 2, 3, 3, 4, 5], Unique, Dup).
I get:
Unique = [2, 4|_G1954],
Dup = [1, 3|_G1948].
I can't figure out why towards the end of both lists I am getting '_G1954
' and '_G1948
'. Any help would be appreciated. Thanks.