Haise Sasaki
Haise Sasaki

Reputation: 329

Return values in Prolog

I'm supposed to write a predicate that does some math stuff. But I don't know how to pass numbers or return numbers.

Maybe you can give me an example?

Let's say a predicate divide/2 that takes two numbers a and b and returns a/b.

Upvotes: 2

Views: 1693

Answers (4)

Reema Q Khan
Reema Q Khan

Reputation: 878

Another simpler approach:

divideList([],_,[]).
divideList([H|T],A,[H1|L]):-
     H1 is H//A,!,
    divideList(T,A,L).

?-divideList([22,4,56,38],2,Answer).
Answer = [11, 2, 28, 19]

Upvotes: 0

Reema Q Khan
Reema Q Khan

Reputation: 878

Here's another approach. So let's say you have a list [22,24,34,66] and you want to divide each answer by the number 2. First we have the base predicate where if the list is empty and the number is zero so cut. Cut means to come out of the program or just stop don't go to the further predicates. The next predicate checks each Head of the list and divides it by the number A, meaning (2). And then we simply print the Answer. In order for it to go through each element of the list we send back the Tail [24,34,66] to redo the steps. So for the next step 24 becomes the Head and the remaining digits [34,66] become the Tail.

divideList([],0,0):-!.
divideList([H|T],A,Answer):-
    Answer is H//A,
    writeln(Answer),
    divideList(T,A,_).

?- divideList([22,24,34,66],2,L).
OUTPUT:
11
12
17
33

Upvotes: 0

Enigmativity
Enigmativity

Reputation: 117175

Prolog is all about unifying variables. Predicates don't return values, they just succeed or fail.

Typically when a predicate is expected to produce values based on some of the arguments then the left-most arguments are inputs and the right-most are the outputs. However, many predicates work with allowing any argument to be an input and any to be a output.

Here's an example for multiply showing how it is used to perform divide.

multiply(X,Y,Z) :- number(X),number(Y),Z is X * Y.
multiply(X,Y,Z) :- number(X),number(Z),X \= 0,Y is Z / X.
multiply(X,Y,Z) :- number(Y),number(Z),Y \= 0,X is Z / Y.

Now I can query it like this:

?- multiply(5,9,X).
X = 45 .

But I can easily do divide:

?- multiply(5,X,9).
X = 1.8 .

It even fails if I try to do a division by 0:

?- multiply(X,0,9).
false.

Upvotes: 1

Will Ness
Will Ness

Reputation: 71119

Yes, you pass numbers in in some arguments, and you get the result back in some other argument(s) (usually last). For example

divide( N, D, R) :-
    R is N / D.

Trying:

112 ?- divide(100,5,X).
X = 20.
113 ?- divide(100,7,X).
X = 14.285714285714286.

Now, this predicate is divide/3, because it has three arguments: two for inputs and one for the output "information flow".

This is a simplified, restricted version of what a Prolog predicate can do. Which is, to not be that uni-directional.

I guess "return" is a vague term. Expression languages have expressions e-value-ated so a function's last expression's value becomes that function's "return" value; Prolog does not do that. But command-oriented languages return values by putting them into some special register. That's not much different conceptually from Prolog putting some value into some logvar.

Of course unification is more complex, and more versatile. But still, functions are relations too. Predicates "return" values by successfully unifying their arguments with them, or fail to do so, as shown in the other answer.

Upvotes: 3

Related Questions