I want to detect all consecutive duplicate numbers in a list. I am trying this with:
sublist( [], _ ):-!.
sublist( [X|XS], [X|XSS] ) :- sublist( XS, XSS ),!.
sublist( [X|XS], [_|XSS] ) :- sublist( [X,XS], XSS ).
consecutive_duplicates(Y,LS):-
member(Y,LS)
, sublist([Y,Y],LS)
.
Departure:
consecutive_duplicates(X,[1,1,4,3,2,2,4]).
X = 1 ;
X = 1 ;
X = 2 ;
X = 2 ;
false.
However I expected the following output:
X = 1;
X = 2;
What do I need to change?
You can use
append/3
:However this solution can still include duplicates if the same duplicate number appears more than once:
To avoid it you can use
setof/3
:And now the tests get the following output: