Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prolog count the number of times a predicate is true

I want to count the number of times a custom predicate is true. For example, I have the following code:

is_man(john). is_man(alex). ?:-is_man(X). 

X will return john, then if I press semicolon it will also return alex, then false.

I want to build something like:

count(is_man(X), Count). 

And this to return

Count = 2 

How can I do that?

like image 661
Victor Blaga Avatar asked May 19 '11 14:05

Victor Blaga


People also ask

How do you count facts in Prolog?

The simplest way to count them would be to use findall then take the length of the result: findall( _, reserve(bus(_), place(4), datetravel([_,_,_]), info([88032454321, robert,downe])), L), length(L, N). and then N is the count. I fully understand that I have to stop the case, recursion is so.

How do you count the number of elements in a list in Prolog?

We can simply use an if-then-else statement that either increments N is N1+1 , or sets N = N1 , like: count([],0). count([H|Tail], N) :- count(Tail, N1), ( number(H) -> N is N1 + 1 ; N = N1 ).

Is Prolog a predicate?

Prolog predicate is the method to contain the argument and return the boolean values such as true or false. It is a function to operate and return given values, variables, or arguments using a prolog programming language.


1 Answers

In SWI-Prolog:

aggregate_all(count, is_man(X), Count). 
like image 134
Kaarel Avatar answered Oct 05 '22 22:10

Kaarel