prolog 的独特结果 [英] unique results from prolog
问题描述
是否有一种简单的方法可以让 prolog 中的查询只返回每个结果一次?
Is there a easy way to make a query in prolog only return each result once?
例如我正在尝试类似的东西:
for instance I'm trying something like:
deadly(Xn) :- scary(X), Xn is X - 1, Xp is X + 1, not(safe(Xn)), safe(Xp).
deadly(Xp) :- scary(X), Xn is X - 1, Xp is X + 1, not(safe(Xp)), safe(Xn).
deadly(X).
并得到
X = 5
X = 5
X = 5
X = 5
....
对我没用.
推荐答案
您可以做的一件事是将 setof/3
应用于生成解决方案的谓词.但请注意,setof/3
是通过将 sort/2
应用于 bagof/3
传递的结果来实现的(至少在SWI-序言).因此,如果您的解决方案生成器永远运行下去,那么 setof/3
将永远不会被应用...
One thing that you can do is to apply setof/3
to the predicate that generates the solutions. But note that setof/3
is implemented by applying sort/2
to the result delivered by bagof/3
(at least this is the case in SWI-Prolog). So, if your solution generator goes on forever, then setof/3
will never be applied...
所以我想说的是尝试编程以免生成重复项,即在有意义的地方使用剪切 (!).
So I would say that try to program so that duplicates are not generated, i.e. by using the cut (!) where it makes sense.
这篇关于prolog 的独特结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!