Prolog 计算谓词为 true 的次数
我想计算自定义谓词为真的次数。 例如,我有以下代码:
is_man(john).
is_man(alex).
?:-is_man(X).
X
将返回john
,那么如果我按分号它也会返回alex
,然后假
。
我想构建类似的东西:
count(is_man(X), Count).
这个返回
Count = 2
我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 SWI-Prolog 中:
In SWI-Prolog:
对于 ISO 标准 Prolog 解决方案,您可以使用 findall/3 生成所有解决方案的列表,然后将 Count 设置为结果列表的长度。按照您的建议,将其包装到用户定义的谓词 count/2 中可能有点棘手,因为我们需要在 a 中形成 findall/3 的第一个参数方式说明您想要作为 count/2 的第一个参数传递的目标中的任何自由(未绑定)变量。
许多 Prolog 提供“计数器”或其他形式的可变全局值(一种非标准扩展),可以与故障驱动的“循环”结合使用以进行相同的计数。稍微麻烦一点但坚持 Prolog 标准的字母是使用 assert 和 retract 通过调整动态事实来创建您自己的“计数器”。
下面是后一种方法的说明。使其“多线程安全”需要额外的逻辑。
For an ISO standard Prolog solution, you might use findall/3 to produce a list of all solutions, then set Count to the length of the resulting list. It could be a bit tricky to wrap this into a user-defined predicate count/2 as you propose, because we need to form the first argument of findall/3 in a way that accounts for any free (unbound) variables in the goal you want to pass as the first argument of count/2.
Many Prologs provide for "counters" or other forms of mutable global values, a nonstandard extension, that could be used in connection with a failure driven "loop" to make the same count. Slightly more cumbersome but sticking to the letter of the Prolog standard would be to use assert and retract to create your own "counter" by adjusting a dynamic fact.
An illustration of the latter approach follows. Making it "multithread safe" would require additional logic.