Post on 30-Dec-2015
Operators, Functions and Modules 1
Pattern Matching&
Recursion
Operators, Functions and Modules 2
add2 0 b = badd2 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
Operators, Functions and Modules 3
A problemThe following table shows the weekly sales in a company,
Week 0 1 2 3 4 5 6
Sales 15 5 7 18 7 0 5
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 -> Intsales 0 = 15sales 1 = 5sales 2 = 7sales 3 = 18sales 4 = 7sales 5 = 0sales 6 = 5
Operators, Functions and Modules 4
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):
Operators, Functions and Modules 5
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)
Operators, Functions and Modules 6
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 0totalSales n = totalSales (n-1) + sales n
maxSales 0 = sales 0maxSales 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 -> BoolisZero 0 = TrueisZero _ = FalsePrelude> isZero ‘A’
Operators, Functions and Modules 7
More Types
Operators, Functions and Modules 8
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
Operators, Functions and Modules 9
Prelude> ord ‘r’114Prelude> ‘\114’rPrelude> chr (114)rHere is a function for converting a lower case letter to capital.offset :: Intoffset = ord ‘A’ – ord ‘a’toCapital :: Char -> ChartoCapital 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 -> Charord :: Char -> Int
Operators, Functions and Modules 10
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)
Operators, Functions and Modules 11
The operator ++ used above is the concatenation operator.Prelude>putStr “Massey University”Massey UniversityPrelude> putStr “\99u\116”cutPrelude>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”
orangesapplespears
Operators, Functions and Modules 12
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.
Operators, Functions and Modules 13
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:
Operators, Functions and Modules 14
Using Integer and Float answer :: Integer answer=42
Main> answer + 2.8ERROR - Unresolved overloading*** Type : Fractional Int => Int*** Expression : answer + 2.8Prelude> answer + truncate 2.844Prelude> fromInteger answer + 2.844.8Prelude> floor 2.82Prelude> ceiling 2.83Prelude> round 2.83Prelude> truncate 2.82
Operators, Functions and Modules 15
Function to compute average of weekly sales
meanSales :: Integer -> Float
meanSales n =
fromInteger (totalSales n) / fromInteger (n+1)