3
votes

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

1 Answers

4
votes

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.