I want to solve a problem that is I have a Prolog list of elements. If the any of the element frequency is greater than N
then false is return. My expectation like below.
?- frequency([1,2,2,2,5],3).
true.
?- frequency([1,2,2,2,2,5],3).
false.
I have a code for get particular element frequency. Any idea for the problem.
count(_, [], 0) :-
!.
count(X, [X|T], N) :-
count(X, T, N2),
N is N2 + 1.
count(X, [Y|T], N) :-
X \= Y,
count(X, T, N).
Use clpfd!
If we build on auxiliary predicate
list_counts/2
, we can definefrequency/2
like this:Sample queries:
Thanks to clpfd we can ask quite general queries—and get logically sound answers, too!