谓词返回。序言
所以我有多次返回 true 的谓词。
% true returns several times and i need to press ';'
?- get_all_transformed_moves.
true ;
true ;
true.
swi prolog 是否有某种方法可以帮助我运行这个谓词而无需输入“;”?
% Wished version
?- get_all_transformed_moves.
true.
So I have the predicate which returns true several time.
% true returns several times and i need to press ';'
?- get_all_transformed_moves.
true ;
true ;
true.
Is the swi prolog have some method which can help me to run this predicate without typing';'?
% Wished version
?- get_all_transformed_moves.
true.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑以下内容:
如果现在你问:
你会得到
这种情况,因为 prolog 找到了三种方法来满足 likes_something/0 谓词(使用 prolog、haskell 和 erlang),所以它回答了 3 次 true。
这不完全是一个问题;任何时候你都可以按,prolog将停止尝试寻找答案(当有很多结果时,这非常方便)。
同样的事情也会发生在你的谓词上:有三种解决方案,并且按 ;你强迫 prolog 找到它们。正如 Rocha 建议的那样,您可以使用 findall/3。例如,您可以编写:
这将仅返回一个
yes
但是,它不会提供比以前版本更多的信息;相反,它隐藏了这样一个事实:有 3 个解决方案,并且浪费时间尝试寻找更多解决方案,而答案却不会改变。
因此,我认为如果您确实想查看结果,则应该使用 findall/3 :
当然,是否需要查看结果取决于您(或者更确切地说)对于您试图解决的问题:p)
另一种选择是使用剪切:
!/0
例如:
这将阻止 prolog 搜索更多解决方案,而您只会得到一个正确的解决方案。
但请注意,削减可能会很棘手。
总而言之:
如果您希望序言搜索所有答案(如果您决定将它们放入列表中或者您的谓词中有副作用或者您只是想要它):
如果您不想 使用 findall/3搜索更多答案的选项:使用剪切 (!/0)
,否则只需按 Enter 键而不是 ;
Consider the following:
if now you ask:
you will get
this happens because prolog finds three ways to satisfy the likes_something/0 predicate (with prolog, haskell and erlang) so it answers true for three times.
this isn't exactly a problem; at any time you can press and prolog will stop trying to find answers (this is quite handy when there are a lot of results).
the same thing happens to your predicate: there are three solutions and by pressing ; you force prolog to find them all. as Rocha suggested you could use findall/3. For example, you could write:
and this will return just one
yes
however, it doesn't offer more information than the previous version; instead it hides the fact that there are 3 solutions and wastes time trying to find more while the answer wont change.
For that reason I think that you should use findall/3 if you actually want to see the results:
of course, the decision of whether you need to see the results or not is up to you (or rather to the problem you are trying to solve :p)
another option is to use a cut:
!/0
for example:
this will stop prolog from searching for more solutions and you will get just one true.
note however that cuts can be tricky.
All in all:
if you want prolog to search for all the answers (if you decide to put them in a list or you have side-effects in your predicates or if you just want it): use findall/3
if you don't want to have the option to search for more answers: use a cut (!/0)
else just press enter instead of ;
您可以使用 findall/3 谓词:
findall(Object,Goal,List)。
生成满足目标 Goal 的所有对象 Object 的列表。通常,对象只是一个变量,在这种情况下,查询可以理解为:给我一个包含满足目标的所有对象实例的列表。
我想这就是你想要的。
You can use the findall/3 predicate:
findall(Object,Goal,List).
produces a list List of all the objects Object that satisfy the goal Goal. Often Object is simply a variable, in which case the query can be read as: Give me a list containing all the instantiations of Object which satisfy Goal.
I suppose that is what you want.
如果您希望谓词最多成功一次,那么您可以使用 SWI 提供的 Once/1。
在你的例子中:
If you want the predicate to succeed at most once, then you can use once/1 provided with SWI.
In your example: