delete two consecutive constants from a list

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).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top