Rippyae
Rippyae

Reputation: 176

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?

Upvotes: 1

Views: 8824

Answers (2)

siddubaba
siddubaba

Reputation: 13

= 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. 

Upvotes: 0

OmG
OmG

Reputation: 18838

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.

Upvotes: 4

Related Questions