谓词返回。序言

发布于 2024-12-05 17:45:01 字数 282 浏览 0 评论 0原文

所以我有多次返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风铃鹿 2024-12-12 17:45:01

考虑以下内容:

likes(prolog).
likes(haskell).
likes(erlang).

likes_something:-
    likes(_Something).

如果现在你问:

?- likes_something.

你会得到

true ;
true ;
true.

这种情况,因为 prolog 找到了三种方法来满足 likes_something/0 谓词(使用 prolog、haskell 和 erlang),所以它回答了 3 次 true。

这不完全是一个问题;任何时候你都可以按,prolog将停止尝试寻找答案(当有很多结果时,这非常方便)。

同样的事情也会发生在你的谓词上:有三种解决方案,并且按 ;你强迫 prolog 找到它们。正如 Rocha 建议的那样,您可以使用 findall/3。例如,您可以编写:

likes_something:-
   findall(X, likes(X), _).

这将仅返回一个 yes

但是,它不会提供比以前版本更多的信息;相反,它隐藏了这样一个事实:有 3 个解决方案,并且浪费时间尝试寻找更多解决方案,而答案却不会改变。
因此,我认为如果您确实想查看结果,则应该使用 findall/3 :

likes_all(L):-
   findall(X,likes(X),L).

当然,是否需要查看结果取决于您(或者更确切地说)对于您试图解决的问题:p)

另一种选择是使用剪切:!/0
例如:

likes_something:-
   likes(_Something),
   !.

这将阻止 prolog 搜索更多解决方案,而您只会得到一个正确的解决方案。
但请注意,削减可能会很棘手。

总而言之:
如果您希望序言搜索所有答案(如果您决定将它们放入列表中或者您的谓词中有副作用或者您只是想要它):

如果您不想 使用 findall/3搜索更多答案的选项:使用剪切 (!/0)

,否则只需按 Enter 键而不是 ;

Consider the following:

likes(prolog).
likes(haskell).
likes(erlang).

likes_something:-
    likes(_Something).

if now you ask:

?- likes_something.

you will get

true ;
true ;
true.

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:

likes_something:-
   findall(X, likes(X), _).

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:

likes_all(L):-
   findall(X,likes(X),L).

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:

likes_something:-
   likes(_Something),
   !.

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 ;

梦初启 2024-12-12 17:45:01

您可以使用 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.

给我一枪 2024-12-12 17:45:01

如果您希望谓词最多成功一次,那么您可以使用 SWI 提供的 Once/1。

在你的例子中:

?- once(get_all_transformed_moves).
true.

If you want the predicate to succeed at most once, then you can use once/1 provided with SWI.

In your example:

?- once(get_all_transformed_moves).
true.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文