Henry Cho
Henry Cho

Reputation: 770

SICStus Prolog making product/3 rule from sum/3

First of all, this is a homework question, so please just give me a hint!

%Here is a rule that defines sum/3 that returns yes if Z is sum of X and Y
sum(X,Y,Z) :-
  Z is X + Y.
%How can I make product/3
product(X,Y,Z) :- % based on sum/3 above?

Also, how can write a query on product such that it returns the answer of X * Y and not that it's merely true?

Upvotes: 0

Views: 569

Answers (1)

icktoofay
icktoofay

Reputation: 129109

Consider that in mathematics:

x * 0 = 0
x * y = x + x * (y - 1)

That should help you write your rules.

As for a query, you can use something like this to get a result like this:

?- product(5, 3, Result).
Result = 15 ?
yes

In short, if you have an unbound variable in a query, it tries to find a value for that variable such that the predicate succeeds.

Upvotes: 2

Related Questions