问题描述
如何在 bagof、setof 和 findall 之间进行选择?有什么重要的区别吗?哪个最常用,哪个最安全?感谢您的评论/回答.
How does one choose between bagof, setof and findall? Are there any important differences? Which is most commonly used and which is the safest? Thanks for your comments/answers.
我查看了 findall/上的 SWI-Prolog 手册页3
发现它们非常相似.
I checked the SWI-Prolog manual page on findall/3
and found them to be very similar.
推荐答案
这是个好问题!
我不想给出详尽的答案,但我想提出一些思路,以帮助您更好地理解这些谓词:
I am not attempting to give an exhaustive answer, but I would like to suggest a few lines of thought that can help you to understand these predicates better:
- 从可以用什么来表达的意义上来说,考虑这些谓词中哪些更基本.例如,你能用
setof/3
来表达findall/3
吗?反过来呢?这有助于了解您至少需要提供什么来实现所有这些谓词. - 考虑这些谓词保留了哪些声明性属性.例如,找到解决方案的顺序是否会影响结果?对于这些谓词中的哪一个?这些谓词中的任何一个都可以失败吗?在哪些情况下准确?
- 考虑每个谓词的空间和时间复杂度.如果有的话,哪些谓词可以比其他谓词更有效地实现和使用?以什么代价和权衡取舍?
- Think about which of these predicates are more fundamental in the sense of what can be expressed in terms of what. For example, can you express
findall/3
in terms ofsetof/3
? And what about the other way around? This helps to see what you need to provide at least to implement all these predicates. - Think about which declarative properties are preserved by these predicates. For example, does the order in which the solutions are found influence the results? For which of these predicates precisely? Can any of these predicates fail? In which cases precisely?
- Think about the space and time complexity of each of these predicates. Which of these predicates, if any, can be implemented and used more efficiently than others? And at what cost and tradeoffs?
此外,我建议您阅读 Richard O'Keefe 的书Prolog 的工艺,了解有关这些谓词的宝贵信息.
In addition, I recommend you read Richard O'Keefe's book The Craft of Prolog for valuable information about these predicates.
这篇关于如何在Prolog中选择bagof、setof和findall的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!