1 / 24

COMP313A Programming Languages

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

quanda
Download Presentation

COMP313A Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP313A Programming Languages Functional Programming (5)

  2. Lecture Outline • Higher order functions • Functions as arguments • Some recapping and exercises • Some more functions as data and results

  3. 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

  4. test :: Int -> Int -> Int test x y | x >= y = somefun f y | otherwise = 4 where f = addnum 4

  5. 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.

  6. Partial Application of Functions add :: Int -> Int -> Int add x y = x+y 4 5 4

  7. 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?

  8. 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

  9. Types of Partial Function Application add :: Int -> Int -> Int add 2 :: Int ->Int add 2 3 :: Int

  10. 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)

  11. Partial Application of Functionsand Operator Sections elem :: Char -> [Char] -> Bool elem ch whitespace where whitespace is the string “ \t\n”

  12. 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

  13. 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))

  14. 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”

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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)

  20. 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)

  21. 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

  22. f must have a function type t e must have type s f e the result has type t

  23. ord ‘c’ +Int 3Int ord ‘c’ +Int False

  24. 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 ?

More Related