将 Prolog 目标作为输入放在第一个参数位置,returns 这个目标在第二个参数位置成功的次数

put a Prolog goal as input in the first argument position and returns the number of times this goal succeeds in the second argument position

正如标题所说,我想编写一个程序来执行此操作。

例如:

?- count(member(X,[1,2,3]), N). 

N = 3 

Yes

但不仅适用于内置成员,还适用于一些运营商,例如:

?- count(17 =:= 12 + 5, N). 

N = 1 

Yes

有人可以帮助我开始吗?

试试这个:

?- findall(., Goal, Ls), length(Ls, L).

示例:

?- findall(., member(X,[1,2,3]), Ls), length(Ls, L).
L = 3,
... .

library(aggregate) 已经实现,为您的问题提供解决方案,还有更多...

?- aggregate(count, X^member(X,[1,2,3]), N).
N = 3.

?- aggregate(count, 17 =:= 12 + 5, N).
N = 1.