序言中的切割处理 [英] cut processing in prolog

查看:36
本文介绍了序言中的切割处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下以下命令的处理过程:

can somebody please explain the processing of the following command:

max(X,Y,Max) :- X >= Y, !, Max = X ; Max = Y. 

看不懂,中间的!到底是什么意思,先谢谢了

I can't understand, what exactly does it mean ! in the middle, thanks in advance

推荐答案

!(cut) 意味着如果你做到了这一步(在当前目标中成功),不要做任何在剪切位置之前可能已经打开的回溯(考虑其他方法来满足当前目标).

The ! (cut) means if you got this far (successfully in the current goal), don't do any backtracking (consider alternative ways to satisfy the current goal) that might have been open before the cut's placement.

在您的示例中,切割用于简洁地表达如何定义 X 和 Y 的最大值.也就是说,一旦您成功实现子目标 X >= Y,正确的替代方法是不再有疑问(因此将 Max 与 X 统一).

In your example the cut is being used to tersely express how to define the Max of X and Y. That is, once you succeed with the subgoal X >= Y, the right alternative is no longer in doubt (so unify Max with X).

这里比较棘手的是,(代表逻辑AND)和;(代表逻辑OR)的优先级.后者比前者绑定更弱,因此只有在 X >= Y 失败时才能达到将 Max 与 Y 统一的替代子目标(如果切割,则不会考虑当前目标的替代方案)已通过).

The tricky thing here is the precedence of , (representing logical AND) and ; (representing logical OR). The latter binds more weakly than the former, so that the alternative subgoal unifying Max with Y can only be reached if X >= Y fails (the alternative for the current goal will not be considered if the cut is passed).

这篇关于序言中的切割处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆