• Tidak ada hasil yang ditemukan

ARITHMETIC EXPRESSION AND OPERATORS IN PROLOG

N/A
N/A
Protected

Academic year: 2023

Membagikan "ARITHMETIC EXPRESSION AND OPERATORS IN PROLOG"

Copied!
17
0
0

Teks penuh

(1)

ARITHMETIC EXPRESSION AND OPERATORS IN PROLOG

Artificial Intelligence Lab Md. Tarek Habib

Department of Computer Science and Engineering Daffodil International University

(2)

ARITHMETIC EXPRESSION AND OPERATORS

Arithmetic Evolution:

Simple arithmetic operators such as ( + or * ) are valid Prolog atoms.

Therefore, expressions like +(3,5) are valid Prolog terms.

They can also be written as infix operators , like in 3+5.

(3)

ARITHMETIC EXPRESSION AND OPERATORS

Arithmetic Evolution:

Without specifically telling Prolog that we are interested in the arithmetic properties of such a term, these expressions are treated purely syntactically i.e. their values are not evaluated.

That means, using ( = ) won’t work the way that you might have expected.

(4)

ARITHMETIC EXPRESSION AND OPERATORS

Arithmetic Evolution:

Example:

?- 3 + 5 = 8.

False.

Here, the term 3+5 and 8 don’t match, the former is compound term whereas the letter is a number

(5)

ARITHMETIC EXPRESSION AND OPERATORS

Arithmetic Evolution: (is) Operator

To check the result, we first have to tell Prolog to arithmetically evaluate the term ( 3 + 5 ).

This is done by using built-in operator (is).

Example:

?- 8 is 3+5.

True.

Again, we can match by the variable with another number.

?- X is 3+5 , X = 8.

X = 8.

?- X is 3+5 , X = 8.

X = 8.

(6)

ARITHMETIC EXPRESSION AND OPERATORS

Arithmetic Evolution: (is) Operator

Again,

We can query like,

If we write like,

?- 3+5 is X False.

Because, is only cause the argument to it’s right and tries to match the result with the left hand argument.

?- X is 3+5.

X = 8

?- X is 3+5.

X = 8

(7)

ARITHMETIC EXPRESSION AND OPERATORS

Arithmetic Evolution: (is) Operator

For Multiplication,

?- X is 3 * 8.

X = 24.

For Subtraction,

?- X is 3 - 5.

X = -2

For Division,

?- X is 6/2.

X = 3

Use // for integer division.

Use // for integer division.

(8)

ARITHMETIC EXPRESSION AND OPERATORS

Predefined Arithmetic Functions and Relations:

The arithmetic operators available in Prolog:

Functions,

Relations

(9)

ARITHMETIC EXPRESSION AND OPERATORS

Functions:

Consider an Expression like,

2 + ( -3.2 * X – max ( 17, X ) ) / 2 * * 5

Here, max /2 expression evaluates to the largest of it’s two arguments

and

2 * * 5 means 2 to the power 5.

(10)

ARITHMETIC EXPRESSION AND OPERATORS

Some other Functions:

min/2 ( minimum ) ,

abs/1 ( absolute value ) ,

sqrt/1 ( square root ) ,

sin/1 ( sine ) ,

round/1 ( round a float number to an integer) ,

mod/2 ( module ).

Can be used only on the right side is

operator.

Can be used only on the right side is

operator.

(11)

ARITHMETIC EXPRESSION AND OPERATORS

Query with Functions:

max/2:

?- X is max (2 , 4).

X = 4.

min/2:

?- X is min ( 2 , 4 ).

X = 2.

abs/1:

?- X is abs( 2.4) X = 2.4

(12)

ARITHMETIC EXPRESSION AND OPERATORS

Query with Functions:

sqrt/1:

?- X is sqrt( 4 ).

X = 2.

round/1:

?- X is round(2.44).

X = 2.

mod/2:

?- X is mod(4 , 2).

X = 0.

?- X is round(2.64).

X = 3.

?- X is round(2.64).

X = 3.

?- X is mod(4 , 3).

X = 1.

?- X is mod(4 , 3).

X = 1.

(13)

ARITHMETIC EXPRESSION AND OPERATORS

Relations:

Arithmetic relations are used to compare two evaluated arithmetic expressions.

Example: The goal X > Y succeeds .if expression X evaluates to be a greater number than expression Y.

Arguments are evaluated whenever an arithmetic relation is used.

(is) operator isn’tneeded (is) operator isn’tneeded

(14)

ARITHMETIC EXPRESSION AND OPERATORS

Relations:

Besides > (greater) the operator < (lower), Available operator are,

=< (lower or equal) ,

>= (greater or equal) ,

=\= (non equal) ,

=:= (equal).

(15)

ARITHMETIC EXPRESSION AND OPERATORS

Relations:

The former compares two evaluated arithmetic expressions and later performs logical pattern matching.

Example:

?- 2 ** 3 =:= 3+5.

True.

Again,

?- 2 ** 3 =:= 2+5.

False.

Differentiation of =:=

and = is crucial.

Differentiation of =:=

and = is crucial.

False. Because the matching returns

false.

False. Because the matching returns

false.

(16)

ARITHMETIC EXPRESSION AND OPERATORS

Relations:

?- 2 ** 5 =\= 2+4.

True

?- 2 ** 5 =\= 2+4.

True

?- 2 ** 5 >= 2+4.

True

?- 2 ** 5 >= 2+4.

True

?- 2 ** 5 >= 2+4.

True

?- 2 ** 5 >= 2+4.

True

?- 2 ** 5 =< 2+4.

False.

?- 2 ** 5 =< 2+4.

False.

(17)

END OF ARITHMETIC EXPRESSION AND OPERATORS

THANK YOU!

Referensi

Dokumen terkait

Results Selection of genes for functional CRISPR screens We selected genes using the following approaches: 1 184 high-confidence INQUISIT level 1 INQ_1 target genes [3], 2 678

1 No payments for any establishment or service shall be made out of the said moneys in excess of the rates voted for similar establishments or services on the Estimates for the