I am trying to write a recursive rule collCount/2 which groups identical items in a list with their respective numbers of occurrences into tuples.
For example, collCount([a,b,a,b,c,b],F) binds F with [(a,2),(b,3),(c,1)]. When running this query, Prolog simply returns no.
The following is what I have managed to do so far:
collCount([H|T],[(H,N)|L2]) :-
countDel(H,[H|T],L,N),
collCount(L,L2).
countDel(X,T,Rest,N) :-
occur(X,T,N),
delAll(X,T,Rest).
occur(_,[],0).
occur(X,[X|T],N) :-
occur(X,T,NN),
N is NN + 1.
occur(X,[H|T],N) :-
occur(X,T,N),
X \= H.
delAll(_,[],[]).
delAll(X,[X|T],Ans) :-
delAll(X,T,Ans).
delAll(X,[H|T],[H|Rest]) :-
delAll(X,T,Rest),
X \= H.
The predicate countDel/4 counts and deletes all occurrences of a specific item in a list. For instance, countDel(2,[1,2,3,2,2],L,N) binds L with [1,3] and N with 3.
The predicate occur/3 counts all occurrences of a specific item in a list. For instance, occur(3,[1,2,3,4,3],Num) binds Num with 2.
The predicate delAll/3 deletes all occurrences of a specific item in a list. For instance, delAll(3,[1,2,3,4,3],L) binds L with [1,2,4].
Any help is greatly appreciated.