150 likes | 268 Vues
Pattern Matching & Recursion. Here is a simple use of patterns in defining a function add2 :: Int -> Int -> Int. add2 0 b = b add2 a 0 = a add2 a b = a + b A pattern can be. A literal 0 ‘a’ True False
E N D
Pattern Matching & Recursion Lecture 5 Pattern Matching & Recursion
Here is a simple use of patterns in defining a function add2 :: Int -> Int -> Int 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 Lecture 5 Pattern Matching & Recursion
A problem The following table shows the weekly sales in a company, 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 Lecture 5 Pattern Matching & Recursion
Definition of totalSales (using guards): 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 Lecture 5 Pattern Matching & Recursion
Function to calculate maximum sales(using guards): maxSales :: Int -> Int maxSales n | n = = 0 = sales 0 | otherwise = maxi (sales n)maxSales (n-1) maxSales 4 = maxi (sales 4)maxSales 3 = maxi 7(maxi (sales 3) maxSales 2) = maxi 7(maxi18 (maxi (sales 2) maxSales 1)) = maxi 7(maxi18 (maxi7 (maxi (sales 1) maxSales 0))) = maxi 7(maxi18 (maxi7 (maxi5 sales 0))) = maxi 7(maxi18 (maxi7 (maxi5 15))) = maxi 7(maxi18 (maxi7 15)) = maxi 7(maxi18 15) = maxi 718 = 18 Lecture 5 Pattern Matching & Recursion
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’ Lecture 5 Pattern Matching & Recursion
More Types Lecture 5 Pattern Matching & Recursion
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 ‘\”’ The ASCII code of the characters can also be used for representing them. For instance, ‘\65’ is equivalent to ‘A’. ASCII codes 65 to 90 represent A-Z ASCII codes 97 to 122 represent a-z ASCII codes 48 to 57 represent 0-9 Lecture 5 Pattern Matching & Recursion
There are two useful built in conversion functions. chr :: Int -> Char ord :: Char -> Int 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. Lecture 5 Pattern Matching & Recursion
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) Lecture 5 Pattern Matching & Recursion
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” The operator ++ used above is the concatenation operator. Prelude>putStr “Massey University” Massey University Prelude> putStr “\99u\116” cut Prelude>putStr “oranges\napples\npears” oranges apples pears Lecture 5 Pattern Matching & Recursion
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. Lecture 5 Pattern Matching & Recursion
Some built-in floating point operations: Float ->Float -> Float +, *, -, /, ** (example: x ** y ) Float ->Float cos, sin, tan, abs, sqrt (positive square root) Float ->Int -> Float ^ (example: x^n) Float ->Int ceiling, floor, round, truncate Int ->Float fromInt Float pi (the constant pi) An example: sin (pi/2) * pi Lecture 5 Pattern Matching & Recursion
Using Int and Float answer :: Int answer=42 Main> answer + 2.8 ERROR - Unresolved overloading *** Type : Fractional Int => Int *** Expression : answer + 2.8 Prelude> answer + truncate 2.8 44 Prelude> fromInt 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 Lecture 5 Pattern Matching & Recursion
Function to compute average of weekly sales meanSales :: Int -> Float meanSales n = fromInt (totalSales n) / fromInt (n+1) Lecture 5 Pattern Matching & Recursion