Hello, I need some help please..

I need to write a recursive predicate in Prolog that subtracts two numbers until number 2 is higher than the result.

I have completed the logic in C++:

``````#include <iostream>

using namespace std;
int mod3(int a, int b)
{
int intResult = a-b;

if(intResult > b)
{
return mod3(intResult, b);
}

return intResult;
}
int main(int argc, char *argv[]) {

cout << mod3(23, 5);

return 0;
}``````

BUT putting it into Prolog is doing my brain in.. This is what I've got so far:

``````modulo(X, M, Y) :-

Y is (X-M),
(Y > M),
modulo(X, M, Y).``````

Could someone give me some help/pointers on what I'm doing wrong?

Thanks :)

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by phorce

Without actually running it, looks infinite recursion as last predicate is same as lhs of the rule Put another variable there, same variable can not unify with two values (forget assignment same varible name means exactly same value in Prolog) actually thinking about it the last statement is junk, remove it. Only add ground rule when Y <= M

Edited by pyTony: n/a

Do you mean something like this:

``````checkNum(Num1, Num2) :-
Num1 > Num2.

modulo(A, B, Result) :-

Result is (A-B),
checkNum(Result, B),``````

?
BEGIN

Result = A-B,
check to see if the result is more than be,
[CONFUSED WHAT TO DO HERE]
END

I think you have to put result back modulo, because it takes 5 from the result..

Basic logical definition of mod/3 is

``````mod(X, Y, X) :- X < Y.
mod(X, Y, Z) :- plus(X1, Y, X),  mod(X1, Y, Z).``````

Edited by pyTony: n/a

Hello,

``````mod(X, Y, X) :- X < Y.