Search code examples
prologpredicatemodulo

Mod or Predicate always fails


I am making a prolog program, and I need to find out if a number is even or odd. I am trying to make a predicate that takes in a number, mods it and if it succeeds it's even, if it fails it's odd.

My code looks like this:

modNum(num):-
    0 =:= mod(num, 2).

But even if I enter 6, it's false. If I enter 3, it's false. I have tried finding a solution online, but none of the answers seem to related to mod so I can't tell if I'm doing something wrong with that part.

Update: I have changed this function to

modNum(num):-
    6 =:= 6.

And it's still failing. I don't know if my mod is still incorrect, but why is my predicate failing when 6 is 6 so should succeed and print true.


Solution

  • In Prolog, variable names must start with an uppercase letter. What you wrote as num is actually a symbol, not a variable. Compare the following typical code:

    loves(hans, lisa).
    loves(lisa, spicy_food).
    

    And now with variables:

    loves(Who, What) :-
        Who = hans,
        What = lisa.