'How to check if a number is even?

I have a predicate that checks for even. However, when I perform querying, it always returns false. I'm new to prolog, and I'm really puzzled by this behavior

Even(N):- N mod 2 = 0. 

Update: If I change it to Even(N):- 0 is N mod 2., then it works. Why is this the case?



Solution 1:[1]

You are not using the proper operators and have some typos! First, the name of the predicate starts with a small letter (even instead of Even). Operator for the equality comparison is =:= (you are using = that is for unification! and is to apply a value to a variable. Although what you right means 0 is 0 for the even numbers and works here but in some situation will be failed. See here to know more about this.).

even(N):- mod(N,2) =:= 0.

Solution 2:[2]

= is for unification and =:= nd is are considered equals to in Prolog. It's as simple as that. Also, whether it's a rule or fact, it always starts so that the code will be:

even(N):-
    mod(N,2) =:= 0. 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jeremy Caney