序言,测试(X,Y,Z):- Y 是 X + Z
当我只知道X时,如何在序言中得到Y和Z?
例如:
test(X, Y, Z) :- X is Y + Z.
但是错误:
?- test(2, Y, Z).
ERROR: is/2: Arguments are not sufficiently instantiated
How to get Y and Z in prolog, when I only know X?
For example:
test(X, Y, Z) :- X is Y + Z.
but error:
?- test(2, Y, Z).
ERROR: is/2: Arguments are not sufficiently instantiated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的,因为您可以选择
Y
为您想要的任何内容,而它们会计算Z
,反之亦然。尽管如果您知道
Y
和Z
来自某个有限集合(例如小于 5 的正整数),您可以执行以下操作:It's not possible, because you can choose
Y
to be anything you want and them computeZ
or vice versa.Although if you know that
Y
andZ
are from some limited set (e.g. positive integers less than 5), you can do something like:您必须将它们作为参数传递。 Prolog 算术(
is/2
)不是一根魔杖,它的右参数必须在计算之前完全实例化(无变量)。如果您希望谓词在多个“方向”上工作,具有基本项和变量的多种组合,您将需要使用约束逻辑编程,但这是逻辑编程的一个相当高级的领域。在有限域上的 CLP 中,你可以说
You have to pass them as arguments. Prolog arithmetic (
is/2
) is not a magic wand, its right argument must be fully instantiated (no variables) before it can be evaluated.If you want the predicate to work in several "directions", with multiple combinations of ground terms and variables, you'll want to use Constraint Logic Programming, but that's a rather advanced area of logic programming. In CLP on finite domains, you can say