prologprolog-toplevel

Prolog, predicate returns the correct result, but also false?


I found that for most of my predicates, prolog finds multiple solutions, with one of them being the correct result, the other is 'false.'

To demontrate:

%finds the last element of list, or false if empty.
last([H|L], More):-
    last(L, More).
last([H], H).

running this gives:

?- last([a, b, c], W).
W = c ;
false.

Can anyone explain why it is giving 'false.' in addition? Is this something I need to fix?


Solution

  • You're not doing anything wrong, nor this is something to fix. What prolog is telling you by printing false at the end is that there are no other solutions than the ones it has already shown you. (Note that hitting ; tells prolog to show you more answers, you can also hit return to simply terminate the query.)

    See Section 2.1.3 of https://www.swi-prolog.org/download/stable/doc/SWI-Prolog-8.2.1.pdf for details.