Вопрос

I need to count all X, that some_predicate(X) and there really a lot of such X. What is the best way to do that?

First clue is to find it all, accumulate to a list and return it length.

countAllStuff( X ) :-
    findall( Y
           , permutation( [1,2,3,4,5,6,7,8,9,10], Y )
           , List
           ),
    length( List, X ).

(permutation/2 is only example showing that there are many variants and it's bad way to collect it all)

Obviously, I have stack-overflow.

?- countAllStuff( X ).
ERROR: Out of global stack

Than, I'm trying to replace findall to setof and nothing changes.

At last, I've founded aggregate(clickable) predicates and trying to use it.

?- aggregate(count, permutation([1,2,3,4], X), Y ).
X = [1, 2, 3, 4],
Y = 1 .

?- aggregate(count, [1,2,3,4], permutation([1,2,3,4], X), Y ).
X = [1, 2, 3, 4],
Y = 1 ;
X = [1, 2, 4, 3],
Y = 1 ;

It's all wrong, I think. I'm prefer to get something like

?- aggregate(count, permutation([1,2,3,4], X), Y ).
Y = 24 .

1) What am I doing wrong?

2) How can I declare predicate to get right answer?

Нет правильного решения

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top