Page 212 - DCAP506_ARTIFICIAL_INTELLIGENCE
P. 212
Artificial Intelligence
Notes Specifically, the thought of evaluating an arithmetic expression is in difference to the straight
pattern matching we have observed so far. For this reason, Prolog offers the incorporated
predicate ‘is’ that assesses arithmetic expressions. Its syntax calls for the utilization of operators.
X is <arithmetic expression>
The variable X is fixed to the value of the arithmetic expression. On backtracking it is unallocated.
The arithmetic expression appears like an arithmetic expression in any other programming
language.
Here is how to make use of Prolog as a calculator.
?- X is 2 + 2.
X = 4
?- X is 3 * 4 + 2.
X = 14
Parentheses clarify precedence.
?- X is 3 * (4 + 2).
X = 18
?- X is (8 / 4) / 2.
X = 1
In addition to ‘is,’ Prolog offers a number of operators that contrast two numbers. These comprise
‘greater than’, ‘less than’, ‘greater or equal than’, and ‘less or equal than.’ They perform more
logically, and succeed or fail as per whether the contrast is true or false.
Notice the order of the symbols in the greater or equal than and less than or equal operators.
They are particularly constructed not to appear like an arrow, so that you can use arrow symbols
in your programs without confusion.
X > Y
X < Y
X >= Y
X =< Y
Example: Here are a few examples of their use.
?- 4 > 3.
yes
?- 4 < 3.
no
?- X is 2 + 2, X > 3.
X = 4
206 LOVELY PROFESSIONAL UNIVERSITY