If you need to know how much X satisfied by some predicate, you do not need to know all of them. Using findall/3 really redundant in such tasks. When you have 6 or 606 of these X - this is not very important. But when you have a really big and heavy generator, you do not need to save all the values ββin a list and then count the length.
Aggregate solves this problem well:
numberr(1). numberr(3). numberr(6). numberr(8). numberr(9). numberr(12). countNumbers( Numbers ) :- aggregate( count, X^numberr( X ), Numbers ).
X^ means "there is X", so the whole formula means something like "count the number X, which numberr(X) , and call that number Numbers .
So,
?- countNumbers(X). X = 6.
source share