Prolog 计算谓词为真的次数 [英] Prolog count the number of times a predicate is true

查看:50
本文介绍了Prolog 计算谓词为真的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算自定义谓词为真的次数.例如,我有以下代码:

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 将返回 john,然后如果我按分号,它也会返回 alex,然后返回 false.

X will return john, then if I press semicolon it will also return alex, then false.

我想构建类似的东西:

count(is_man(X), Count).

然后返回

Count = 2

我该怎么做?

推荐答案

在 SWI-Prolog 中:

In SWI-Prolog:

aggregate_all(count, is_man(X), Count).

这篇关于Prolog 计算谓词为真的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆