# Prolog Operators _ Arithmetic Reading Arithmetic Operators Meaning

Document Sample

```					                       Prolog Operators & Arithmetic
week           date                 Monday                      Tuesday             Thursday
1        Jan. 9           Introduction               Haskell Start-Up   Haskell Start-Up               Rest of Chapter 3: Sections 3 & 4
2        Jan 16           Haskell Start-Up           Recursion          Lists and Tuples
(assn 1 due)
Skip part of Section 3: defining your own operators
4        Jan 30           Proofs/review              Quiz 1             I/O
(assn 2 due)
5        Feb 6            Algebraic Types            Algebraic Types    Generalization
6        Feb 13           Functions As Values        Type Classes       Type Checking & Inference
7        Feb 27           Haskell review             Quiz 2             Lazy Programming
(assn 3 due)
(assn 4 due Friday)
10        Mar 20           review / arithmetic        Quiz 3             arithmetic
11        Mar 27                                      (assn 5 due)

12        Apr 3                                                          (assn 6 due)

CISC 260, winter 2006, Prolog: Operators & Arithmetic                                            1     CISC 260, winter 2006, Prolog: Operators & Arithmetic                2

Arithmetic Operators                                                                                 Meaning of = Operator
Prolog provides the standard infix operators for arithmetic:                                          Asks if two terms match exactly.
+, -, *, /                                                                                         What matches 2+4?
Shorthand for structure notation.
2+4 is the same as: +(2,4)                                                                         Only answer: 2+4.
Not 6.
Important: 2+4 is just a structure                                                                    Not 4+2.
Using it does not perform an addition!                                                                Moral: = tests structural equality, not arithmetic equality.
Using 2+4 is not the same as using 6.                                                                    experiment with = and \= in SWI-Prolog,
example from quiz

Section 3.3 tells you how to define your own infix operators.
You may skip that part.

CISC 260, winter 2006, Prolog: Operators & Arithmetic                                            3     CISC 260, winter 2006, Prolog: Operators & Arithmetic                4
Inequalities                                               New Equality Operator
Prolog provides inequality operators:                                     =:= tests for arithmetic equality
>      strictly greater                                               like inequality operators: forces arithmetic evaluation
<      strictly less                                                  operands must be numeric
>=     greater than or equal to                                       no unbound variables
=<     less than or equal to
These operators do force arithmetic evaluation.
2+4 > 5-1 is true.                                                        =\= tests for arithmetic "not equals"
If operands aren't numbers, you get an error message.                        try these out in SWI-Prolog
experiment with inequalities in SWI-Prolog,
including bound & unbound variables

CISC 260, winter 2006, Prolog: Operators & Arithmetic                 5   CISC 260, winter 2006, Prolog: Operators & Arithmetic               6

"is" Operator                                Summary of Comparison Operators
X = 2+4-1 sets X equal to a structure, doesn't evalute.
Three "equality" operators:
Sometimes you would like an assignment that evaluates the                 =    checks for exact structural match, no arithmetic evaluation
right hand side.                                                       =:= evaluates both sides, checks for numeric equality
Use "is":                                                                 is evaluates RHS, assigns to variable on LHS
X is 2+4-1.
Now X has the value 5.
Two "not equals" operators:
Rules for "is":                                                           \=    two sides are not structurally equal
• right hand side must be bound and numeric                           =\= evaluates both sides, results are not numerically equal
• left hand side should be a variable
• if variable is bound, checks for numeric equality (like
=:=)                                                             Other numeric comparisons: <, >, >=, =<
• if variable is unbound, evaluates right hand side and               Always evalute both sides.
assigns result to variable
try "is" with SWI-Prolog
CISC 260, winter 2006, Prolog: Operators & Arithmetic                 7   CISC 260, winter 2006, Prolog: Operators & Arithmetic               8
Simple Application: count Predicate                                  More Arithmetic Operators/Functions
Want count(Item,List,N) to mean Item occurs exactly                   / = full division (result may be integer or floating point)
N times in List.                                                    // = truncated division (result will be integer)
mod = remainder, just like in Java or Haskell
round = rounds argument to nearest integer
try in SWI-Prolog

CISC 260, winter 2006, Prolog: Operators & Arithmetic             9   CISC 260, winter 2006, Prolog: Operators & Arithmetic                  10

Another Counting Problem                                            Answer To Student Question
Want total(L,N) to mean L contains a total of N elements,             Is there any way to ask for name & arguments of structure?
possibly in nested lists                                           Two SWI-Prolog functions: functor & arg
Example:                                                              Not required – just for your information
length([ [a,b], [c,d,e] ], 2).
total([ [a,b], [c,d,e] ], 5).                                      functor(Term,FunctorName,NumParams)
% fails if Term is not a structure
Some useful built-in predicates (see SWI-Prolog manual)
atom(X)           is X an atom (non-numeric only)?                 arg(N,Term,Value)
atomic(X)         is X an atom (numbers okay here)?                % Value is Nth parameter, counting from 1
number(X)         is X a number?
integer(X) is X an integer?                                        Challenge: extend total predicate so it looks at structures as
var(X)            is X an unbound variable?                           well as lists:
is_list(X) is X a list?                                            total(a,[a,name(a,b),[a]],3).

CISC 260, winter 2006, Prolog: Operators & Arithmetic           11    CISC 260, winter 2006, Prolog: Operators & Arithmetic                  12

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 1126 posted: 3/31/2010 language: Indonesian pages: 3