Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operators, Functions and Modules1 Pattern Matching & Recursion.

Similar presentations


Presentation on theme: "Operators, Functions and Modules1 Pattern Matching & Recursion."— Presentation transcript:

1 Operators, Functions and Modules1 Pattern Matching & Recursion

2 Operators, Functions and Modules2 add2 0 b = b add2 a 0 = a add2 a b = a + b A pattern can be A literal 0 ‘a’ True False A variable any argument value will match this Wild card “_” any argument value will match this A tuple pattern (p1, p2, …, pn) matches a tuple with matching values A constructor Here is a simple use of patterns in defining a function add2 :: Int -> Int -> Int

3 Operators, Functions and Modules3 A problem The following table shows the weekly sales in a company, Week0123456 Sales155718705 We want to know that given a week n, What is the total sales from week 0 to week n What is the maximum weekly sales from week 0 to week n Function to calculate total sales given a particular week: We will first represent the table of weekly data, sales :: Int -> Int sales 0 = 15 sales 1 = 5 sales 2 = 7 sales 3 = 18 sales 4 = 7 sales 5 = 0 sales 6 = 5

4 Operators, Functions and Modules4 totalSales :: Int -> Int totalSales n | n = = 0 = sales 0 | otherwise = totalSales (n-1) + sales n We have used recursion in the definition of our functions. This type of recursion is called primitive recursion. The first clause is called the base case and the second clause is called the recursive case. totalSales 4 = totalSales 3 + sales 4 = (totalSales 2 + sales 3) + sales 4 = ((totalSales 1 + sales 2) + sales 3 )+ sales 4 = (((totalSales 0 + sales 1) + sales 2) + sales 3 )+ sales 4 = ((((sales 0 + sales 1) + sales 2) + sales 3 ) + sales 4 = 15+5 + 7+18+7+0+5= 57 Definition of totalSales (using guards):

5 Operators, Functions and Modules5 maxSales 4 = maxi (sales 4) maxSales 3 = maxi 7 (maxi (sales 3) maxSales 2) = maxi 7 (maxi 18 (maxi (sales 2) maxSales 1)) = maxi 7 (maxi 18 (maxi 7 (maxi (sales 1) maxSales 0))) = maxi 7 (maxi 18 (maxi 7 (maxi 5 sales 0))) = maxi 7 (maxi 18 (maxi 7 (maxi 5 15))) = maxi 7 (maxi 18 (maxi 7 15)) = maxi 7 (maxi 18 15) = maxi 7 18 = 18 Function to calculate maximum sales(using guards): maxSales :: Int -> Int maxSales n | n = = 0 = sales 0 | otherwise = maxi (sales n) maxSales (n-1)

6 Operators, Functions and Modules6 Pattern matching could have been used for defining these functions. Pattern matching may be used when we have a number of equations. Each equation can become a pattern. Let’s now define totalSales and maxSales using patterns. totalSales 0 = sales 0 totalSales n = totalSales (n-1) + sales n maxSales 0 = sales 0 maxSales n = maxi (sales n) (maxSales (n-1)) The underscore “_” (known as don’t care) can be used as a pattern and we use it when we do not care about the value it matches with. isZero :: Int -> Bool isZero 0 = True isZero _ = False Prelude> isZero ‘A’

7 Operators, Functions and Modules7 More Types

8 Operators, Functions and Modules8 The ASCII code of the characters can also be used for representing them. For instance, ‘\65’ is equivalent to ‘A’. Character Type Char is a Haskell built-in type. Characters are put inside single quotes. ‘a’ to ‘z’, ‘A’ to ‘Z’, ‘0’ to ‘9’ Some characters are represented using a backslash “\” before them. Examples are: tab ‘\t’, newline ‘\n’, backslash ‘\\’ single quote ‘\’’, double quote ‘\”’ ASCII codes 65 to 90 represent A-Z ASCII codes 97 to 122 represent a-z ASCII codes 48 to 57 represent 0-9

9 Operators, Functions and Modules9 Prelude> ord ‘r’ 114 Prelude> ‘\114’ r Prelude> chr (114) r Here is a function for converting a lower case letter to capital. offset :: Int offset = ord ‘A’ – ord ‘a’ toCapital :: Char -> Char toCapital ch = chr (ord ch + offset) Here, we did not have to define offset. We could have simply said: toCapital ch = chr (ord ch + (ord ‘A’ – ‘ord ‘a’)) It is however good practice to define offset as ewe have. There are two useful built in conversion functions. chr :: Int -> Char ord :: Char -> Int

10 Operators, Functions and Modules10 Other functions toLowr:: Char -> Char toLowr ch = chr (ord ch - offset) isDigit :: Char -> Bool isDigit ch = (‘0’ <= ch) && (ch <= ‘9’) isChar :: Char -> Bool isChar ch = not (isDigit ch)

11 Operators, Functions and Modules11 The operator ++ used above is the concatenation operator. Prelude>putStr “Massey University” Massey University Prelude> putStr “\99u\116” cut Prelude>putStr “oranges\napples\npears” Strings: A string is a special list consisting of characters. Type String = [Char] Here are some strings: “Haskell is a functional programming language.” ”\72el\108o\t world” “Haskell “ ++ “ programming” oranges apples pears

12 Operators, Functions and Modules12 Floating Point Numbers Type Float is used for calculations involving floating point numbers. Examples of floating point numbers: 110.3421 345.365 4.0 -2.09 Type Float is not used very much. Floating point arithmetic is not very precise in Haskell because of the limited space allowed for the internal representation of floating point numbers. A type like Double may be used for more precision.

13 Operators, Functions and Modules13 Float -> Float -> Float +, *, -, /, ** (example: x ** y ) Float -> Float cos, sin, tan, abs, sqrt (square root) Float -> Int -> Float ^ (example: x^n) Float -> Int ceiling, floor, round, truncate Integer -> Float fromInteger Float pi (the constant pi) An example: sin (pi/2) * pi Some built-in floating point operations:

14 Operators, Functions and Modules14 Using Integer and Float answer :: Integer answer=42 Main> answer + 2.8 ERROR - Unresolved overloading *** Type : Fractional Int => Int *** Expression : answer + 2.8 Prelude> answer + truncate 2.8 44 Prelude> fromInteger answer + 2.8 44.8 Prelude> floor 2.8 2 Prelude> ceiling 2.8 3 Prelude> round 2.8 3 Prelude> truncate 2.8 2

15 Operators, Functions and Modules15 Function to compute average of weekly sales meanSales :: Integer -> Float meanSales n = fromInteger (totalSales n) / fromInteger (n+1)


Download ppt "Operators, Functions and Modules1 Pattern Matching & Recursion."

Similar presentations


Ads by Google