Prolog Operators

Prolog Lists

Built-In Predicates

Miscellaneous

Prolog - Arithmetic Comparison Operators



Arithmetic Comparison operators are specifically used to compare numeric values of arithmetic states or expressions. Arithmetic expressions are evaluated before being compared using comparsion operators. Following is the list of arithmetic comparison operators −

Operator Meaning
X > Y X is numerically greater than Y
X < Y X is numerically less than Y
X >= Y X is numerically greater than or equal to Y
X =< Y X is numerically less than or equal to Y
X =:= Y X and Y values are numerically equal
X =\= Y X and Y values are not numerically equal

You can see that the '=<' operator, '=:=' operator and '=\=' operators are syntactically different from other languages.

Example

GNU Prolog 1.5.0 (64 bits)
Compiled Jul  8 2021, 12:33:56 with cl
Copyright (C) 1999-2021 Daniel Diaz

| ?- 1+2=:=2+1
.

yes
| ?- 1+2=2+1.

no
| ?- 1+A=B+2.

A = 2
B = 1

yes
| ?- 5<10.

yes
| ?- 5>10.

no
| ?- 10=\=100.

yes
| ?-

Here we can see 1+2=:=2+1 is returning true, but 1+2=2+1 is returning false. This is because, in the first case it is checking whether the value of 1 + 2 is same as 2 + 1 or not, and the other one is checking whether two patterns 1+2 and 2+1 are same or not. As they are not same, it returns no (false). In the case of 1+A=B+2, A and B are two variables, and they are automatically assigned to some values that will match the pattern.

Advertisements