1 / 19

COMP313A Functional Programming (4)

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.

Télécharger la présentation

COMP313A Functional Programming (4)

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. COMP313AFunctional Programming (4)

  2. Lecture Outline • A little bit of revision • Higher Order Functions • Functions as arguments • Functions as values

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

  4. 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 <- , ]

  5. Map double x = 2 * x doubleAll xs = map double xs • doubleAll [4, 5, 6] • [8, 10, 12]

  6. Implementing Map using List Comprehension map :: (a -> b) -> [a] -> [b] map f xs = [ f x | x <- xs ]

  7. Functions as Argumentsfold > foldr1 (+) [4, 5, 6] > 15

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

  9. foldr f s [] = s foldr f s (x:xs) = f x (foldr f s xs)

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

  11. Implementing Filter using list comprehension filter p xs = [x | x <- xs, p x] p returns a Bool

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

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

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

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

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

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

  18. Main> addNum 4 5 9 Main> addNum 4 ERROR - Cannot find "show" function for: *** Expression : addNum 4 *** Of type : Integer -> Integer

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

More Related