Working on a prolog assignment.
I have a structure cnt(letter,number).
I need to return a list of cnt
where each cnt
is the number of times each character appears (assuming that each item has already been sorted to place the same item one after each other).
I have this so far:
cnt(letter,number).
freq([],[]).
freq([A|L],Y) :- grab(A,Init,_), freq(L,[Init|Y]).
grab works correctly takes a list of items and returns the list of the first duplicates as Init
e.g grab([a,a,a,b,c], Init, Rest).
will return Init = [a,a,a]
.
Assuming I have a list [a,a,a,b,b,b,c,c]
i need freq to return Y = [cnt(a,3), cnt(b,3), cnt(c,2)].
I think what I have so far is near correct, except that it returns false.
Is there anyway to step through to see what it is doing to get there? Or can anyone see any obvious problems.