Why does Prolog wait

邮差的信 提交于 2019-12-23 18:04:29

问题


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.

I wrote:

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

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

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.

example:

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

.

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

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


回答1:


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

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



来源:https://stackoverflow.com/questions/33766233/why-does-prolog-wait

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!