Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do return both a variable result and a true/false in Prolog?

It sounds silly, but lets say my predicate largest/2 returns the largest element in a list...the output should look like this:

?- largest([1,2,3,4,5], X).
X = 5.
false.

I implemented largest, and it works like above except it doesn't output "false". How do I make it so it also outputs this "false." value? This is for an annoying assignment I have to finish. :(

like image 616
Eteocles Avatar asked Dec 28 '22 04:12

Eteocles


2 Answers

That extra false. or No just means that the person running the program asked to get all possible solutions for X, not just the first possible solution.

On most interactive Prolog interpreters, you check to see if there is another solution by pressing the semicolon (;) key.

like image 53
aschepler Avatar answered Feb 01 '23 22:02

aschepler


sounds like impossible, as if predicate fails, no binding of free variables happens, see

 ?- A=5.
A = 5.

 ?- A=5,false.
false.

however

 ?- A=5;false.
A = 5 ;
false.

To achieve this you should make your predicate "largest" non-deterministic. But to me this seems pretty silly.

like image 41
Volodymyr Gubarkov Avatar answered Feb 02 '23 00:02

Volodymyr Gubarkov