190 likes | 343 Views
COMP313A Functional Programming (4). Lecture Outline. A little bit of revision Higher Order Functions Functions as arguments Functions as values. Some more examples pattern matching. Write a function which will extract all the even numbers from a list.
E N D
Lecture Outline • A little bit of revision • Higher Order Functions • Functions as arguments • Functions as values
Some more examplespattern matching Write a function which will extract all the even numbers from a list Lets first create an isEven predicate isEven n = (mod n 2 == 0) evenList [] = [] evenList ex = [n | n <- ex , isEven n] evenList2 [] = [] evenList2 (x:xs) |isEven x = x : evenList2 xs |otherwise = evenList2 xs
List Comprehension Given a function isDigit isDigit :: Char -> Bool which returns True if a character is a digit, write a function digits which will find all the digits in a string digits str = [ aChar | aChar <- , ]
Map double x = 2 * x doubleAll xs = map double xs • doubleAll [4, 5, 6] • [8, 10, 12]
Implementing Map using List Comprehension map :: (a -> b) -> [a] -> [b] map f xs = [ f x | x <- xs ]
Functions as Argumentsfold > foldr1 (+) [4, 5, 6] > 15
foldr1 (non empty list) foldr1:: (a -> a -> a) -> [a] -> a foldr1 f [x] = x foldr1 f (x:xs) = f x (foldr f xs) What does this tell us abut the characteristics of function “f” Produces an error when given an empty list How could we define foldr to work with an empty list
foldr f s [] = s foldr f s (x:xs) = f x (foldr f s xs)
1 More Higher Order FunctionFilter isEven n = (mod n 2 == 0) > filter isEven [2, 4, 6, 7, 1] >?? isEven returns a predicate (returns a Bool)
Implementing Filter using list comprehension filter p xs = [x | x <- xs, p x] p returns a Bool
Some exercises • Write functions to • Return the list consisting of the squares of the integers in a list, ns. • Return the sum of squares of items in a list • Check whether all the items of a list are greater than zero using filter
Functions as values • functions as data • function composition sqr (succ 5) concat (map bracketedWithoutVowels xs) concatenates a list of lists into a single list flattens a list
Functions as valuesFunction Composition (.) (sqr . succ) 5 • The output of one function becomes the input of another f.g a a b c c g f (.) :: (b -> c) -> (a ->b) -> (a -> c) type of (f.g) type of f type of g
f . g x as opposed to (f . g) x Function application binds more tightly than composition e.g not . not True or succ .pred 5
Functions as values and results twice fun = ( fun . fun ) • fun is a function • The result is fun composed with itself For this to work fun has to have ….. and twice :: ( ) -> ( ) > twice succ 2 > 4
Expressions Defining Functions addnum :: Int -> (Int -> Int) addnum n = addN where addN m = n + m When addnum 10 say is called returns a function addN which adds 10 to m
Main> addNum 4 5 9 Main> addNum 4 ERROR - Cannot find "show" function for: *** Expression : addNum 4 *** Of type : Integer -> Integer
test :: Int -> Int -> Int test x y | x >= y = f y | otherwise = 4 where f = addnum 4