countprologmeta-predicate

put a Prolog goal as input in the first argument position and returns the number of times this goal succeeds in the second argument position


As the title says I want to write a program that does this.

an example would be:

?- count(member(X,[1,2,3]), N). 

N = 3 

Yes

But not only for the build in member, but also for some operators like:

?- count(17 =:= 12 + 5, N). 

N = 1 

Yes

Can someone help me get started?


Solution

  • Try this:

    ?- findall(., Goal, Ls), length(Ls, L).

    Example:

    ?- findall(., member(X,[1,2,3]), Ls), length(Ls, L).
    L = 3,
    ... .