140 likes | 314 Views
Operators and Functions. Both prefix and infix notations are allowed in Haskell. infix notation: a + b + is called an operator prefix notation: mod a b mod is called a function An operator can be used in expressions just like a function and vice versa.
E N D
Operators and Functions Operators, Functions and Modules
Both prefix and infix notations are allowed in Haskell. infix notation: a + b + is called an operator prefix notation: mod a b 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 Prelude>9 `div` 3 3 Operators, Functions and Modules
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 = True | otherwise = False Main> 4 <->2 True Main> 4 <->3 False Operators, Functions and Modules
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 Non associative operators are either left or right associative in Haskell. Operators, Functions and Modules
Binding Power Associativity can help in resolving ambiguity in the use of an operator. When more than one operator is involved then the binding power (or infixity) is used to resolve ambiguity. 18 – 4 * 2 = 10 binding power “*”= 7 ; “–” , “+” = 6 2 ^ 3 ^ 2 = 512 ^ is right associative 4 ^ 3 * 2 = 128 The maximum possible binding power is 9 ^ is right associative and has a binding power of 8 *, / are left associative and have a binding power of7 +, – are left associative and have a binding power of 6 ++ is right associative and has a binding power of 5 /=, <, <= , >, >= are non-associative and have a binding power of 4 You can change the associativity or binding power of an operator. Operators, Functions and Modules
Setting theassociativity 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
Binding power of function applications is the highest. So, fact n + 1 means (fact n) + 1. If we wanted it to mean factorial of (n+1) then we would say, fact (n + 1). Negative Numbers Because minus is used both as the subtraction and negation operators, fact –1 is interpreted as fact minus 1 and not, fact (-1). Use brackets when you are not sure. Operators, Functions and Modules
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 = True | otherwise = False infixr 4 <-> Main> 24 <-> 3*6 False Operators, Functions and Modules
Layout Operators, Functions and Modules
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 scripts is used to tell where a function ends and another begins. fact n = 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) This layout rule is called the offside rule. Operators, Functions and Modules
Modules and Imports Operators, Functions and Modules
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 + offset) else chr (ord ch - offset) Operators, Functions and 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
module test where-- import from module Fact import fact-- function to find the number of combinations 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.hs d:\haskell\compiler\test.hs Test> comb 3 2 result will be 3 Operators, Functions and Modules