صفحه 1:
Operators and Functions
Operators, Functions and
Modules
صفحه 2:
Both prefix and infix notations are allowed in Haskell.
infix notation: و + + is called an
operator
prefix notation: modab mod is called a function
An operator can be used in expressions just like a
function and vice versa.
Prelude> 2*4
Studie )*( 2 4
0 953
000 3 ‘div’ 2
ERROR - Improperly terminated character constant ب
Operators, Functions and
Prelude>9 * div’ 3 Modules
صفحه 3:
You may even define your own infix operators in the
same way as
functions. You may not begin an operator with a “:”.
Example:
(<->) :: Int -> Int -> Bool
a <-> b
| mod a b= = = True
| otherwise = False
Main> 4 <-> 2
True
Main> 4 <-> 3
Operators, Functions and 3
False Modules
صفحه 4:
Assoc: ity
Addition is associative because the order of its
application does not
matter. In an expression like 1+2+3 we may write:
14+ (2 +3) or ۲1 ۰ (3
without effecting the results. That is why we do not
0 eae ‘with subtraction as it is
not associative.
3-2-1
Left associative: (3-2)-1=0
right associative: 3-(2-1)=2
Non associative operators pirectither left or right 4
aeaenriative in Hackell
صفحه 5:
Binding Power
iativity can help in resolving ambiguity in the use of an ope
more than one operator is involved then the binding powe
fixity) is used to resolve ambiguity.
pk =10 binding power “*”= 7; “-”, ‘
~2 = 512 ~ is right associative
22 < 8
1aximum possible binding power is 9
is right associative and has a binding power df
are left associative and havea _ binding power o|
are left associative and havea binding power af
is right associative and has a binding power of
<=,>,>= are non-associative and have a binding power pf
an change the associativity or binding power of an operato
Operators, Functions and
Modules
صفحه 6:
Setting the associativity and/or binding power of an
operator:
You can use declarations of the form,
infixr binding-power operator
infixl binding-power operator
infix binding-power operator
Examples:
infixr 7 <->
infixl 5 <->
We may even do this with backquoted function names.
infixr 8 ‘fact
Operators, Functions and 6
Modules
صفحه 7:
ding power of function applications is the highest. So,
factn +1
1
(fact n) + 1.
wanted it to mean factorial of (n+1) then we would say,
fact (n + 1).
ive Numbers
se minus is used both as the subtraction and negation ope
fact -1
rpreted as fact minus 1 and not,
fact (-1).
rackets when you are not sure.
Operators, Functions and 7
Modules
صفحه 8:
Main> 24 >-< 6
ERROR - Unresolved overloading
ee Type : Num Bool => Bool
*«* Expression : 24 <-> 3 *6
Adding the proper declaration will solve the problem.
(<->) :: Int -> Int -> Bool
a<>b
| mod a b= = = True
| otherwise = False
infixr 4 <->
Main> 24 <-> 3*6
Operators, Functions and 8
False Modules
صفحه 9:
Layout
Operators, Functions and
Modules
صفحه 10:
What determines where one function ends and the
other begins in a Haskell script ? This may be done
explicitly by using a semicolon (“;”) at the end of a
definition. Using semicolons, you can put more than
one definition on a line.
answer = 20; done = False
We normally do not use semicolons. The layout of
Faetle scripts is used to tell where a function ends
angrotidefibegins.
maximn
|m>n
The following layout will result in error
addi x=x+
1
ERROR “add1.hs":2 - Syntax error in expression
(unexpected °;', possibly due to bad layout) i
This layout rule is calfed taareftside rule.
صفحه 11:
11
Modules and Imports
Operators, Functions and
Modules
صفحه 12:
module Fact where
-- function to find the factorial of a given integer
fact :: Integer -> Integer
fact n = product [1..n]
-- function to find the square of a number
squ :: Integer -> Integer
2*0 ع ظ 50
{- Function to change the case of a given character -}
offset: :Int
offset=ord 'A' - ord ‘a’
changeCase:: Char -> Char
changeCase ch = if (ord ch > 96) then chr(ord ch te
offset) else chr (ord Ch ORgaey°" فم
صفحه 13:
Prelude> :] d:haskell\compiler\fact.hs
Reading file "d:haskell\
compiler\fact.hs":
Hugs session for:
C:\Program Files\Hugs98\\
lib\Prelude.hs
d:haskell\compiler\fact.hs
Fact>fact 3
6
Fact>fact 4
24
Fact>
Operators, Functions and 13
Modules
صفحه 14:
-- import from module Fact
-- function to find the
er
comb nr = factn div (fact r* fact (n - r))
Prelude> :] d:haskell\compiler\fact.hs
Reading file "d:haskell\compiler\
fact.hs":
Hugs session for:
C:\Program Files\Hugs98\\lib\
Prelude.hs
d:haskell\compiler\fact.hs
Fact> :1 d:\haskell\compiler\test.hs
Hugs session for:
C:\Program Files\Hugs98\\lib\
Prelude.hs
d:\haskell\compiler\fact.h$)perators, Functions and 5
d:\haskell\compiler\test.hs Modules