I have a list and I should to delete consecutive duplicates in the list.
So for example:
compress([a,a,b,c,c,d,d,d,e], New_List)
New_List = [a,b,c,d,e].
I have write some code, but when I try I receive false, and don't understand why (I'm a little bit newbie in prolog).
compress([],[]).
compress([Head| Rest], New_List):-
compress(Head, Rest, New_List).
compress([], [], []).
compress(Head, [Head_N | Rest_N], New_List) :-
Head \== Head_N,
append([Head], New_List),
compress(Head_N, Rest_N, New_List).
compress(Head, [Head_N | Rest_N], New_List) :-
Head == Head_N,
compress(Head_N, Rest_N, New_List).
EDIT 1.
I noticed right now, while i'm writing, that with this code the last letter will never append.
You can make a helper predicate that drops elements of a list until it finds the next element that is different, or reaches the end of the list:
dropEq([], _, []).
dropEq([H|T], X, [H|T]) :-
dif(H, X).
dropEq([H|T], H, R) :-
dropEq(T, H, R).
next we can use this to compress the list:
compress([], []).
compress([H|T], [H|R]) :-
dropEq(T, H, Q),
compress(Q, R).