Prolog: “findall” for limited number of solutions

后端 未结 9 880
暖寄归人
暖寄归人 2020-12-06 17:36

Say I want to find sum of all solutions to a predicate, I just can use

findall(L, find(L), Sols),

and just sum members of Sols.

But

9条回答
  •  借酒劲吻你
    2020-12-06 18:14

    I've just realized that in B-Prolog it's trivial to do via tabling:

    %% an example predicate with infinite solutions
    %% from ChristianF answer.
    find(0).
    find(X) :- find(X1), X is X1+1.
    
    :- table find(-).
    
    ?- table_cardinality_limit(find/1, 10), findall(X, find(X), L).
    L = [0,1,2,3,4,5,6,7,8,9]
    yes
    

提交回复
热议问题