0

I am trying to write my on mod function in Prolog, with a signature of modulo(A,B,C), where C is equivalent to A mod B. This is my code:

modulo(A,B,A) :- A<B. /* Not sure about this part.*/

modulo(A,B,C) :-
A==C -> 
    true ;
    A>C ->
        A1 is A-B,
        modulo(A1,B,C) ;
        false .

For some reason, when I run this code, it gives me an "Arguments are not sufficiently instantiated" error. I am very new to Prolog, and I was wondering if someone could please help me figure out what I am doing wrong/how to fix it?

Any help would be greatly appreciated! Thank you in advance!

1
  • Prolog doesn't have functions. That might be your first mistake. Commented Apr 4, 2020 at 4:49

1 Answer 1

1

You seem to be trying to get a Prolog predicate to return either true or false. That's not how Prolog works. A predicate either succeeds or it fails. They don't return anything.

Other than that, your code is close.

The first case you need to cover is when A < B.

modulo(A,B,A) :- A < B.

If A < B and the first and the third terms are the same then this predicate should succeed.

The next case is when A >= B.

modulo(A,B,C) :-
    A >= B,
    A1 is A - B,
    modulo(A1,B,C).

And that's it.

Notice I haven't returned anything. All we want to say is "did this succeed".

Trying on ?- modulo(7,7,1). I get No., but trying on ?- modulo(7,2,1). I get Yes..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.