尝试在 Prolog 中定义运算符时出现问题
我用以下代码定义了一个 prolog 文件:
divisible(X, Y) :-
X mod Y =:= 0.
divisibleBy(X, Y) :-
divisible(X, Y).
op(35,xfx,divisibleBy).
Prolog 抱怨说
'$record_clause'/2:无权修改 static_procedure `op/3'
我做错了什么?我想定义一个 divisibleBy 运算符,它允许我编写如下代码:
4 divisibleBy 2
谢谢。
I have defined a prolog file with the following code:
divisible(X, Y) :-
X mod Y =:= 0.
divisibleBy(X, Y) :-
divisible(X, Y).
op(35,xfx,divisibleBy).
Prolog is complaining that
'$record_clause'/2: No permission to modify static_procedure `op/3'
What am I doing wrong? I want to define an divisibleBy operator that will allow me to write code like the following:
4 divisibleBy 2
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
:-
告诉 Prolog 解释器在加载文件时评估下一个术语,即进行谓词调用,而不是将其视为定义(在本例中是重新定义)op/3
)。Use
:-
tells the Prolog interpreter to evaluate the next term while loading the file, i.e. make a predicate call, instead of treating it as a definition (in this case a redefinition ofop/3
).@larsmans 给出的答案对于您原来的问题是准确的。
但是,您应该重新考虑是否您应该定义一个新的运算符。
一般来说,我强烈建议不要定义新的运算符,原因如下:
The answer given by @larsmans is spot-on regarding your original problem.
However, you should reconsider if you should define a new operator.
In general, I would strongly advise against defining new operators for the following reasons: