Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operators, Functions and Modules1 Operators and Functions.

Similar presentations


Presentation on theme: "Operators, Functions and Modules1 Operators and Functions."— Presentation transcript:

1 Operators, Functions and Modules1 Operators and Functions

2 Operators, Functions and Modules2 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 Both prefix and infix notations are allowed in Haskell.

3 Operators, Functions and Modules3 Example: ( ) :: Int -> Int -> Bool a b | mod a b= = 0 = True | otherwise = False Main> 4 2 True Main> 4 3 False You may even define your own infix operators in the same way as functions. You may not begin an operator with a “:”.

4 Operators, Functions and Modules4 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. 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.

5 Operators, Functions and Modules5 ^ is right associative and has a binding power of 8 *, / are left associative and have a binding power of 7 +, – 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 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 You can change the associativity or binding power of an operator. Binding Power

6 Operators, Functions and Modules6 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` Setting the associativity and/or binding power of an operator: You can use declarations of the form,

7 Operators, Functions and Modules7 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. Binding power of function applications is the highest. So,

8 Operators, Functions and Modules8 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 Main> 24 3*6

9 Operators, Functions and Modules9 Layout

10 Operators, Functions and Modules10 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.

11 Operators, Functions and Modules11 Modules and Imports

12 Operators, Functions and Modules12 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)

13 Operators, Functions and Modules13 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>

14 Operators, Functions and Modules14 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


Download ppt "Operators, Functions and Modules1 Operators and Functions."

Similar presentations


Ads by Google