Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Prolog wait

Tags:

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.

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.

like image 545
Sefi Erlich Avatar asked Apr 11 '26 18:04

Sefi Erlich


1 Answers

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.

like image 85
repeat Avatar answered Apr 15 '26 22:04

repeat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!