I'm trying to define a predicate count(Word,List,N)
which is true when N is the
number of times that the word Word occurs in list List. For example
count(my, [the, friend, of, my, enemy, is, not, my,enemy,nor,my,friend], N)
should be true for N = 3.
The following is what I tried so far:
count(Word, [], 0).
count(Word, [Word|Tail], N) :-
count(Word, Tail, NofTail), N is 1 + NofTail.
count(Word, [OtherWord|Tail], NofTail) :-
OtherWord \= Word, count(Word, Tail, NofTail).
When I tried it in RPEL:
?- count(s,[s,z],1).
true ;
false. %note here
?- count(s, [s,s,s,z], 3).
true ;
false. %note here
?- count(s, [s,s,s,z], 1).
false.
Am I doing right? If not, why? How to fix it?