صفحه 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

Operators and Functions Operators, Functions and Modules 1 Both prefix and infix notations are allowed in Haskell. infix notation: operator prefix notation: a+b mod a b + is called an mod is called a function An operator can be used in expressions just like a function and vice versa. Prelude> 2*4 8 Prelude> (*) 2 4 8 Prelude>div 9 3 3 Prelude> 3 ‘div’ 2 ERROR - Improperly terminated character constant Operators, Functions and Prelude>9 `div` 3 Modules 2 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= = 0 | otherwise = True = False Main> 4 <-> 2 True Main> 4 <-> 3 False Operators, Functions and Modules 3 Associativity Addition is associative because the order of its application does not matter. In an expression like 1+2+3 we may write: 1+ ( 2 + 3) or (1 + 2) + 3 without effecting the results. That is why we do not use the brackets in such cases. Doing the same is not possible with subtraction as it is not associative. 3–2–1 Left associative: (3 – 2) – 1 = 0 right associative: 3 – (2 – 1) = 2 Operators,are Functions and left or right Non associative operators either Modules 4 Binding Power ciativity can help in resolving ambiguity in the use of an ope n more than one operator is involved then the binding power fixity) is used to resolve ambiguity. 4*2 ^2 *2 = 10 = 512 = 128 binding power “*”= 7 ; “–” , “ ^ is right associative maximum possible binding power is 9 , <= , >, >= is right associative and has a binding power of are left associative and have a binding power of are left associative and have a binding power of is right associative and has a binding power of are non-associative and have a binding power of can change the associativity or binding power of an operator Operators, Functions and Modules 5 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 Modules 6 ding power of function applications is the highest. So, fact n + 1 s (fact n) + 1. wanted it to mean factorial of (n+1) then we would say, fact (n + 1). tive Numbers use minus is used both as the subtraction and negation oper fact –1 erpreted as fact minus 1 and not, fact (-1). rackets when you are not sure. Operators, Functions and Modules 7 Main> 24 <-> 3*6 ERROR - Unresolved overloading *** Type : Num Bool => Bool *** Expression : 24 <-> 3 * 6 Adding the proper declaration will solve the problem. (<->) :: Int -> Int -> Bool a <-> b | mod a b= = 0 | otherwise = True = False infixr 4 <-> Main> 24 <-> 3*6 False Operators, Functions and Modules 8 Layout Operators, Functions and Modules 9 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 Haskell fact n = scripts is used to tell where a function ends and another begins. product [1..n] maxi m n |m>n … The following layout will result in error add1 x = x + 1 ERROR "add1.hs":2 - Syntax error in expression (unexpected `;', possibly due to bad layout) Operators, Functions and 10 This layout rule is called Modules the offside rule. Modules and Imports Operators, Functions and Modules 11 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 squ n = n*n {- 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 + Operators, Functions and 12 offset) else chr (ord ch - offset) Modules Prelude> :l 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 Modules 13 module test where -- import from module Fact import fact number of combinations -- function to find the comb :: Integer -> Integer -> Integer comb n r = fact n `div` (fact r * fact (n - r)) Prelude> :l 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> :l d:\haskell\compiler\test.hs Hugs session for: C:\Program Files\Hugs98\ \lib\Prelude.hs d:\haskell\compiler\fact.hsOperators, Functions and d:\haskell\compiler\test.hs Modules 14

51,000 تومان