返回两个或多个结果时成功的谓词 [英] Predicate that succeeds if two or more results are returned
问题描述
如果rule2
返回两个或多个结果,如何实现成功的rule1
?
rule1(X) :-规则 2(X, _).
如何计算结果,然后设置何时成功的最小值?
如何计算结果,然后设置为真的最小值?
不清楚您所说的结果是什么意思.所以我会做一些猜测.结果可能是:
一个解决方案.例如,目标 member(X,[1,2,1])
有两个解决方案.不是三个.在这种情况下,请考虑使用 setof/3
或类似的谓词.无论如何,在解决您遇到的问题之前,您应该先了解setof/3
.
答案.目标 member(X,[1,2,1])
有三个答案.目标 member(X,[Y,Z])
有两个答案,但有无穷多个解.
因此,如果您想确保至少有一定数量的答案,请定义:
<预>至少(目标,N):-\+ \+ call_nth(目标,N).使用 call_nth/2
在另一个 SO-answer 中定义.
请注意,其他 SO 答案是不正确的:它们要么不会终止,要么不会产生意外的实例.
How to implement rule1
that succeeds iff rule2
returns two or more results?
rule1(X) :-
rule2(X, _).
How can I count the results, and then set a minimum for when to succeed?
How can I count the results, and then set a minimum for when it's true?
It is not clear what you mean by results. So I will make some guesses. A result might be:
A solution. For example, the goal member(X,[1,2,1])
has two solutions. Not three. In this case consider using either setof/3
or a similar predicate. In any case, you should first understand setof/3
before addressing the problem you have.
An answer. The goal member(X,[1,2,1])
has three answers. The goal member(X,[Y,Z])
has two answers, but infinitely many solutions.
So if you want to ensure that there are at least a certain number of answers, define:
at_least(Goal, N) :- \+ \+ call_nth(Goal, N).
with call_nth/2
defined in another SO-answer.
Note that the other SO-answers are not correct: They either do not terminate or produce unexpected instantiations.
这篇关于返回两个或多个结果时成功的谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!