I’m using: SWI-Prolog versionSWI-Prolog version 9.2.9 for arm64-darwin
.
I want the code to:
Solve problem number 8 from P-99: Ninety-Nine Prolog Problems: Eliminate consecutive duplicates of list elements.
Questions:
1 - Isn’t the pattern matching depending on the order of clauses of a multi-clause predicate? (pre-trained brain in Erlang/Elixir/F# style of pattern matching, hence the assumption)
2 - Why is H1 \= H2,
needed in the second solution?
3 - Why the tests for the second solution succeed with a warning Test succeeded with choicepoint
? How to fix it?
My code looks like this:
:- module(workbench, []).
% P08
% 1
% why this one fails with:
% ERROR: Expected: [1,2]
% ERROR: Found: [[1,2],[1,2],[1,2,2],[1,2,2],[1,2,2],[1,2,2],[1,2,2,2],[1,2,2,2],[1,1,2],[1,1,2],[1,1,2,2],[1,1,2,2],[1,1,2,2],[1,1,2,2],[1,1,2,2,2],[1,1,2,2,2],[1,1,2],[1,1,2],[1,1,2,2],[1,1,2,2],[1,1,2,2],[1,1,2,2],[1,1,2,2,2],[1,1,2,2,2],[1,1,1,2],[1,1,1,2],[1,1,1,2,2],[1,1,1,2,2],[1,1,1,2,2],[1,1,1,2,2],[1,1,1,2,2,2],[1,1,1,2,2,2]]
% does the back tracking breaks the assumption about the order of matches (which I inherited from Erlang and similar)?
% what is going on here? why do we need to put the condition (H1 \= H2)?
% no_duplicates([], []).
% no_duplicates([E], [E]).
% no_duplicates([H,H|T], Out) :-
% no_duplicates([H|T], Out).
% no_duplicates([H|T], [H|L1]) :-
% no_duplicates(T, L1).
% 2
% works (with: Test succeeded with choicepoint - how to fix it? why is there more than one answer?)
no_duplicates([], []).
no_duplicates([E], [E]).
no_duplicates([H,H|T], Out) :-
no_duplicates([H|T], Out).
no_duplicates([H1,H2|T], [H1|L1]) :-
H1 \= H2,
no_duplicates([H2|T], L1).
:- begin_tests(test_suite).
test(no_duplicates) :-
no_duplicates([], []),
no_duplicates([1], [1]),
no_duplicates([1,2], [1,2]),
no_duplicates([1,1,1,2,2,2], [1,2]).
:- end_tests(test_suite).
Thanks for your help!