prolog - 为什么 `f(a)=a` 失败但 `f(X)=X` 在 prolog 中成功?
问题描述
我遇到以下情况:
?- f(X) = X.
X = f(X).
?- f(a) = a.
false.
为什么统一适用f(X) = X
,但不适用f(a) = a
?是不是因为第一个简单地说是f(X)
as的名称返回值X
,而第二个试图检查返回值是否f(a)
是a
?但是f()
这里没有定义!!另外,我想,序言中没有“返回值”这样的概念。那么,这里发生了什么?
解决方案
In your first example, X
is a variable (identifier starts with capital letter, look it up). A free variable unifies with anything. (almost anything. you are creating a cyclic term, this will not work if you try to "unify with occurs check", look it up).
In your second example, a
is an atom. It only unifies with a free variable or with itself. Since f(a)
is not a
the unification fails.
You are correct that there is no such thing as "return value". You might treat the success or failure of a goal as the "return value", but I don't know how much this helps.
Either way, there is no f()
in Prolog. This is not a function. You don't need to define it. It is just a compound term (look it up). It is a data structure, in a way.
推荐阅读
- java - java ForkJoinPool compute() 方法中的同步
- javascript - Django html模板中的Javascript代码打印
- java - 如果 ServerConnector 已经配置了 AcceptQueueSize,是否需要为 QueuedThreadPool 指定 BlockingQueue?
- python - 即使我在 KEYUP 期间告诉它停止,我的播放器仍会继续下降。游戏
- c# - VisualStudio 中的 CMake 和 Nuget 包
- python-3.x - 使用客户转换器中的智能函数向数据框添加功能
- r - 将 DALEX 解释函数应用于 xgboost 模型以进行 what_if / centerisParibus 分析
- javascript - 在 JavaScript 中,有没有办法在不使用 toLocaleString 的情况下将日期转换为时区?
- python - 如果我在 __exit__ 方法中返回 self,为什么异常会消失?
- javascript - 为什么 keyDown 事件监听器似乎随机触发了错误的功能?