Since you are trying to solve this with recursion this answer will take that approach. Also this answer will only cover the mode of a list being bound and count being unbound and will not use cuts to remove choice points. You can
enhance the code if desired.
When creating recursive predicates for list I typically start with a template like:
process_list([H|T],R) :-
process_item(H,R),
process_list(T,R).
process_list([],R).
with the recursive case:
process_list([H|T],R) :-
process_item(H,R),
process_list(T,R).
and the base case:
process_list([],R).
The list is deconstructed using [H|T]
where H
is for head of list and T
is for tail of list. R
is for result.
The head item is processed using:
process_item(H,R)
and the tail of the list is processed using:
process_list(T,R)
Since this requires processing two adjacent items in the list modifications are needed:
process_list([H1,H2|T],R) :-
process_item(H1,H2,R),
process_list([H2|T],R).
process_list([],0).
process_list([_],1).
NB There are now two base cases instead of one. Just because recursive predicates are typically one recursion clause and one base case clause does not mean they are always one recursive clause and one base case clause.
Next update the process_item
process_item(I1,I1,N,N).
process_item(I1,I2,N0,N) :-
I1 \== I2,
N is N0 + 1.
Since is/2
is used to increment the count, a count state needs to be passed in, updated and passed out, thus the variables, N0
and N
.
When using state variable or threaded variables, the naming convention is to append 0
to the input value, have no number appended to the output value and increment the appended number in the same clause as the threading progresses.
When the items are the same the count is not incremented which is done using:
process_item(I1,I1,N,N).
When the items are different the count is incremented which is done using:
process_item(I1,I2,N0,N) :-
I1 \== I2,
N is N0 + 1.
In the process of changing process_item
, R
became N0
and N
so this requires a change to process_list
process_list([H1,H2|T],N0,N) :-
process_item(H1,H2,N0,N1),
process_list([H2|T],N1,N).
and to use this a helper predicate is added so that the signature of the original predicate can stay the same.
count(L,N) :-
process_list(L,0,N).
The full code
count(L,N) :-
process_list(L,0,N).
process_list([H1,H2|T],N0,N) :-
process_item(H1,H2,N0,N1),
process_list([H2|T],N1,N).
process_list([],N,N).
process_list([_],N0,N) :-
N is N0 + 1.
process_item(I1,I1,N,N).
process_item(I1,I2,N0,N) :-
I1 \== I2,
N is N0 + 1.
Test cases
:- begin_tests(count).
test(1,[nondet]) :-
count([],N),
assertion( N == 0 ).
test(2,[nondet]) :-
count([a],N),
assertion( N == 1 ).
test(3,[nondet]) :-
count([a,a],N),
assertion( N == 1 ).
test(4,[nondet]) :-
count([a,b],N),
assertion( N == 2 ).
test(5,[nondet]) :-
count([b,a],N),
assertion( N == 2 ).
test(6,[nondet]) :-
count([a,a,b],N),
assertion( N == 2 ).
test(7,[nondet]) :-
count([a,b,a],N),
assertion( N == 3 ).
test(8,[nondet]) :-
count([b,a,a],N),
assertion( N == 2 ).
:- end_tests(count).
Example run
?- run_tests.
% PL-Unit: count ........ done
% All 8 tests passed
true.
Solution using DCG
% Uses DCG Semicontext
lookahead(C),[C] -->
[C].
% empty list
% No lookahead needed because last item in list.
count_dcg(N,N) --> [].
% single item in list
% No lookahead needed because only one in list.
count_dcg(N0,N) -->
[_],
\+ [_],
{ N is N0 + 1 }.
% Lookahead needed because two items in list and
% only want to remove first item.
count_dcg(N0,N) -->
[C1],
lookahead(C2),
{ C1 == C2 },
count_dcg(N0,N).
% Lookahead needed because two items in list and
% only want to remove first item.
count_dcg(N0,N) -->
[C1],
lookahead(C2),
{
C1 \== C2,
N1 is N0 + 1
},
count_dcg(N1,N).
count(L,N) :-
DCG = count_dcg(0,N),
phrase(DCG,L).
Example run:
?- run_tests.
% PL-Unit: count ........ done
% All 8 tests passed
true.
Better solution using DCG.
Side note
In your example code is the use of the ;/2
A typical convention when forammting code with ;/2
is to format as such
(
;
)
so that the ;
stands out.
Your code reformatted
countDistinct([H,H1|T], N) :-
(
(
H == H1,
countDistinct([H1|T], N)
)
;
(
H =\= H1,
N1 is N+1,
countDistinct([H1|T], N1)
)
).