Prolog 中递归后返回一个值
我决定学习一些逻辑编程,然后我偶然发现了一个问题。 它是在 SWI Prolog 中编程的。
test(A, B, N):-
nonvar(B),
B = final,
true.
test(A, B, N):-
N > 2,
test(A, final, N).
test(A, B, N):-
N1 is N + 1,
test(N1, B, N1).
这只是一个样本,没有实际用途,只是它让我发疯。
问题是,当代码达到 true 时,它就会开始回溯并回答“true”。但我需要“返回”值A。我该怎么做?
I decided to study some logic programming and I stumbled across a problem.
It is programmed in SWI Prolog.
test(A, B, N):-
nonvar(B),
B = final,
true.
test(A, B, N):-
N > 2,
test(A, final, N).
test(A, B, N):-
N1 is N + 1,
test(N1, B, N1).
It is just a sample with no real use except it is driving me crazy.
The problem is that when the code reaches true then it starts tracking back and answers "true". But I need to "return" value A. How do I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
A 没有与规则正文中的任何内容统一。 prolog 的工作方式是通过术语的统一。您不能像过程语言中那样“返回”A。例如,当递归结束时,你希望 A 的值是多少?我不知道你的代码在做什么,所以让我使用我自己的例子。
下面是一个 sum 过程,它将对列表中的值求和并“返回 N”,即列表中的值的总和。要调用此过程,您可以这样做:
Prolog 将响应:
请注意,随着递归的进行,accumulate 过程使用 A 作为累加器。也就是说,A 保留运行总和,而 N 是它返回的最终答案。在递归过程中,N 不与任何实际值统一。
在递归的最后一步,即列表为空时,A的值与N统一,实际上返回N。
让我们做一个Trace。
现在,请注意跟踪中我标记
//A 与 _G417(内部变量名称)统一、B 与 _G418 统一、N 与 0 统一的点。
。此时 A 是您的外部变量,_G417 是您的内部 A。如果此调用成功,它最终会执行 prolog 将仅报告外部变量值。在内部,_G417 从来不与其他任何东西统一。我认为问题之一是理解 Prolog 的统一模型如何工作。A is not being Unified with anything in the body of your rules. The way prolog works is via unification of terms. You cannot "return" A as in procedural languages as such. For instance, what do you want the value of A to be when the recursion comes to an end? I have no idea what your code is doing so let me use an example of my own.
Here is a sum procedure that will sum the values in a list and "return N", the sum of the values in the list. To call this procedure you can do this:
And Prolog will respond:
Notice the accumulate procedure is using A as an accumulator as the recursion goes on. That is, A keeps the running sum, while N is the final answer it returns. During the recursion N is not unified with any real value.
In the final step of the recursion, that is, when the list is empty, the value of A is unified with N, in effect returning N.
Let us do a Trace.
Now, notice the point in the trace where I marked
//A unifies with _G417 (internal variable name), B with _G418 and N with 0.
. At that point A is your external variable and _G417 is your internal A. If this call succeeds which it ultimately does prolog will only report the external variable values. Internally _G417 is never unified with anything else. I think the problem is one of understanding how the unification model of Prolog works.我这里没有序言编译器,但您是否尝试过以下内容:
I don't have my prolog compiler here but have you tried something along the lines of: