为什么 Prolog 等待 [英] Why does Prolog wait

查看:50
本文介绍了为什么 Prolog 等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在为我的大学@prolog 做一项工作,我试图找到以下实践的答案:查找它们是否是两个列表之间的公共元素.

So I'm doing a work for my university @prolog, and I'm trying to find a answer to the following practice: find if they're a common element between the two lists.

我写道:

inlist(X,[X|_]).
inlist(X,[H|L]) :-
   inlist(X,L).

isOneInterest([X],[X|_]).
isOneInterest([X|L1],L) :-
   (  inlist(X,L)
   ;  isOneInterest(L1,L)
   ).

现在我知道有更好的解决方案,我会很高兴地看到它们,但我的问题是:为什么 Prolog 在回答 true 后等待?当回答false时,它不等待.

Now I know there are better solutions, and I will see them gladly but my question is: Why does Prolog wait after answering true? When answering false, it doesn't wait.

示例:

11 ?- isOneInterest([a,b],[a,d]).
true.

.

12 ?- isOneInterest([a,b],[s,d]).
false.

在哪里可以看到点(12 点以上),我必须按 Enter.

where you can see the dot (above 12) i had to press enter.

推荐答案

以下内容不是特别针对您的代码,而是针对一般的 Prolog 代码.

The following is not about your code in particular but about Prolog code in general.

您观察到的现象背后的根本原因是:Prolog 目标可以成功多次,但最多只能失败一次.

The fundamental reason behind the phenomenon you observed is this: Prolog goals can succeed more than once, but they can fail at most once.

这篇关于为什么 Prolog 等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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