So I ran into this Prolog problem:
Given a linear numerical list remove all sequences of consecutive values.
E.g.
remove([1, 2, 4, 6, 7, 8, 10], L)
will produceL=[4, 10]
.
I managed to write this code:
consecutive(N,P):-
N is P-1.
removeS([],[]).
removeS([H],[H]).
removeS([H1,H2],[]):-
consecutive(H1,H2).
removeS([H1,H2|T],[H1|L]):-
not(consecutive(H1,H2)),
removeS([H2|T],L).
removeS([H1,H2,H3|T],L):-
consecutive(H1,H2),
not(consecutive(H2,H3)),
removeS([H3|T],L).
removeS([H1,H2,H3|T],L):-
consecutive(H1,H2),
consecutive(H2,H3),
removeS([H3|T],L).
It almost works on some cases like:
removeS([1,2,3,4,5,2,3,4,5,6,7,9,11],R). --> R = [5, 9, 11];
Here only 9 and 11 should be displayed
removeS([3,2,1,2,3,4,5,2,3,4,5,6,7,9,11],R). --> R = [3, 2, 5, 9, 11];
Here only 3,2,9,11 should appear in R
It's that it takes the last number of an increasing sequence into consideration when it computes the next step.
What I am doing wrong here?