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?
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.
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 ).
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.
In SWI-Prolog:
aggregate_all(count, is_man(X), Count).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With