240 likes | 375 Views
COMP313A Programming Languages. Functional Programming (5). Lecture Outline. Higher order functions Functions as arguments Some recapping and exercises Some more functions as data and results. Expressions Defining Functions. addnum :: Int -> (Int -> Int) addnum n = addN
E N D
COMP313A Programming Languages Functional Programming (5)
Lecture Outline • Higher order functions • Functions as arguments • Some recapping and exercises • Some more functions as data and results
Expressions Defining Functions addnum :: Int -> (Int -> Int) addnum n = addN where -- local definition addN m = n + m When addnum 10 say is called returns a function addN which adds 10 to m
test :: Int -> Int -> Int test x y | x >= y = somefun f y | otherwise = 4 where f = addnum 4
Lambda in Haskell \m -> n + m addNum n = (\m -> n + m) Write a function “test n” that returns a function which tests if some argument is <= to n. Use a lambda.
Partial Application of Functions add :: Int -> Int -> Int add x y = x+y 4 5 4
Partial Application of Functions… add4 :: [Int] -> [Int] add4 xs = map (add 4) xs add4 :: ([Int] -> [Int]) add4 = map (add 4) How would we use partial applications of functions to get the same result as the “addNum n” example?
Types of partial applications The type of function is t1 -> t2 -> … tn -> t and it is applied to arguments e1 :: t1, e2 :: t2 … , ek :: tk if k <= n (partial application) then cancel the ones that match t1 – tk leaving tk+1 -> tk+2 -> … -> tn
Types of Partial Function Application add :: Int -> Int -> Int add 2 :: Int ->Int add 2 3 :: Int
Operator Sections (*2) (2*) (>2) (6:) (\2) filter (>0) . map (+1) Find operator sections sec1 and sec2 so that map sec1. filter sec2 Has the same effect as filter (>0) . map (+1)
Partial Application of Functionsand Operator Sections elem :: Char -> [Char] -> Bool elem ch whitespace where whitespace is the string “ \t\n”
Partial Application of Functionsand Operator Sections i.e. whitespace = “ \t\n” The problem with partial application of function is that the argument of interest may not always be the first argument So member xs x = elem x xs and member whitespace Alternatively we can use a lambda function \ch -> elem ch whitespace
Partial Application of Functionsand Operator Sections To filter all non-whitespace characters from a string filter (not . member whitespace) filter (\ch -> not (elem ch whitespace))
Write a recursive function to extract a word from a string whitespace = “ \n\t” getword :: String -> String getword [ ] = [ ] getword (x : xs) | --how do we know we have a word | -- otherwise build the word - - recursively getword “the quick brown”
Write a recursive function to extract a word from a string Can write something more general - pass the “test” as an argument getUntil :: (a -> Bool) -> [a] -> [a] getUntil p [ ] = [ ] getUntil p (x:xs) | p x = [ ] | otherwise = x : getUntil p xs
Write a recursive function to extract a word from a string Okay but now how do we get a word getWord xs = getUntil p xs where - - local definition p x = member whitespace x
Write a recursive function to extract a word from a string But we don’t really need the local definition We can use our partial function instead getWord xs = getUntil p xs where - - local definition p x = member whitespace x getWord xs = getUntil (member whitespace) xs
Write a recursive function to extract a word from a string Finally The last word getWord = getUntil (member whitespace) ---get characters until a whitespace is found
Currying and Uncurrying • functions of two or more arguments take arguments in sequence, one at a time. • this is the curried form. • named after Haskell Curry • Uncurried version puts the arguments into a pair addUC :: (Int, Int) -> Int addUC (x,y) = (x + y)
curried versus uncurried • Curried has neater notation • Curried permits partial application • Can easily convert from one to the other curry f (x y) = f x y uncurry f x y = f (x y)
Type Checking in HaskellMonomorphic Type Checking • Expressions literal, variable, constant, function applied to some arguments • type checking function application • what do we need to consider
f must have a function type t e must have type s f e the result has type t
ord ‘c’ +Int 3Int ord ‘c’ +Int False
Type Checking Function Definitions fib :: Int ->Int fib n | n == 0 = 0 | n == 1 = 1 | n >1 = fib (n-2) + fib (n-1) • Each of the guards must be of type ? • The value returned in each clause must be of type ? • The pattern n must be consistent with type argument ?