functional programming - higher order functions

Download Functional Programming - Higher Order Functions

Post on 11-May-2015

621 views

Category:

Education

3 download

Embed Size (px)

DESCRIPTION

Function order, functions as input parameters. Anonymous functions. Higher order list functions: filter, map, fold.

TRANSCRIPT

  • 1.Functional Programming Higher-Order Functions H. Turgut Uyar 2013-2015

2. License 2013-2015 H. Turgut Uyar You are free to: Share copy and redistribute the material in any medium or format Adapt remix, transform, and build upon the material Under the following terms: Attribution You must give appropriate credit, provide a link to the license, and indicate if changes were made. NonCommercial You may not use the material for commercial purposes. ShareAlike If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. For more information: https://creativecommons.org/licenses/by-nc-sa/4.0/ Read the full license: https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode 3. Topics 1 Higher-Order Functions Function Order Example: Sorting Anonymous Functions Example: Fixed Points 2 List Functions Filter Map Fold Example: Edit Distance 4. Topics 1 Higher-Order Functions Function Order Example: Sorting Anonymous Functions Example: Fixed Points 2 List Functions Filter Map Fold Example: Edit Distance 5. First Class Values first class values can be: assigned composed with other values passed as parameters returned as function results in functional programming, functions are first class values 6. First Class Values first class values can be: assigned composed with other values passed as parameters returned as function results in functional programming, functions are first class values 7. Function Order first order functions only accept data as parameter, and only return data as result higher-order functions take functions as parameters, or return functions as result 8. Function Order first order functions only accept data as parameter, and only return data as result higher-order functions take functions as parameters, or return functions as result 9. First Order Function Examples sum up the squares in a range -- sqr :: Integer -> Integer sumSqr :: Integer -> Integer -> Integer sumSqr a b | a > b = 0 | otherwise = sqr a + sumSqr (a + 1) b sum up the factorials in a range -- fac :: Integer -> Integer sumFac :: Integer -> Integer -> Integer sumFac a b | a > b = 0 | otherwise = fac a + sumFac (a + 1) b 10. First Order Function Examples sum up the squares in a range -- sqr :: Integer -> Integer sumSqr :: Integer -> Integer -> Integer sumSqr a b | a > b = 0 | otherwise = sqr a + sumSqr (a + 1) b sum up the factorials in a range -- fac :: Integer -> Integer sumFac :: Integer -> Integer -> Integer sumFac a b | a > b = 0 | otherwise = fac a + sumFac (a + 1) b 11. Higher-Order Function Example note the pattern sumFun a b | a > b = 0 | otherwise = fun a + sumFun (a + 1) b send the function as parameter sumF f a b | a > b = 0 | otherwise = f a + sumF f (a + 1) b sumSqr a b = sumF sqr a b sumFac a b = sumF fac a b 12. Higher-Order Function Example note the pattern sumFun a b | a > b = 0 | otherwise = fun a + sumFun (a + 1) b send the function as parameter sumF f a b | a > b = 0 | otherwise = f a + sumF f (a + 1) b sumSqr a b = sumF sqr a b sumFac a b = sumF fac a b 13. Higher-Order Function Example what is the type of sumF? sumF :: (Integer -> Integer) -> Integer -> Integer -> Integer 14. Higher-Order Function Example Python def sum_f(f, a, b): total = 0 while a Strategy -> Integer -> Match generateMatch _ _ 0 = ([], []) generateMatch sA sB n = step (generateMatch sA sB (n - 1)) where step :: Match -> Match step (movesA, movesB) = (sA movesB : movesA, sB movesA : movesB) 18. Topics 1 Higher-Order Functions Function Order Example: Sorting Anonymous Functions Example: Fixed Points 2 List Functions Filter Map Fold Example: Edit Distance 19. Sorting in C, qsort takes comparison function as parameter typedef struct { int num, denom; } rational; rational items[] = {{3, 2}, {1, 3}, {2, 1}}; qsort(items, 3, sizeof(rational), compare_rationals); 20. Sorting int compare_rationals(const void *r1, const void *r2) { int num1 = ((rational *) r1)->num; int denom1 = ((rational *) r1)->denom; int num2 = ((rational *) r2)->num; int denom2 = ((rational *) r2)->denom; if (num1*denom2 > num2*denom1) return 1; else if (num1*denom2 < num2*denom1) return -1; else return 0; } 21. Sorting in Python, sorted takes key function as parameter def second(p): return p[1] def value(p): return p[0] / p[1] pairs = [(3, 2), (1, 3), (2, 1)] # sorted(pairs) ~> [(1, 3), (2, 1), (3, 2)] # sorted(pairs, key=second) ~> [(2, 1), (3, 2), (1, 3)] # sorted(pairs, key=value) ~> [(1, 3), (3, 2), (2, 1)] 22. Sorting parameterize qSort regarding comparison function qSort :: (a -> a -> Bool) -> [a] -> [a] qSort precedes [] = [] qSort precedes (x:xs) = qSort precedes before ++ [x] ++ qSort precedes after where before = [a | a [5, 4, 3] -- qSort ( ["a", "b", "c"] 23. Topics 1 Higher-Order Functions Function Order Example: Sorting Anonymous Functions Example: Fixed Points 2 List Functions Filter Map Fold Example: Edit Distance 24. Anonymous Functions no need to name small functions that are not used anywhere else anonymous functions x1 x2 ... -> e example sumSqr :: Integer -> Integer -> Integer sumSqr a b = sumF (x -> x * x) a b 25. Anonymous Functions no need to name small functions that are not used anywhere else anonymous functions x1 x2 ... -> e example sumSqr :: Integer -> Integer -> Integer sumSqr a b = sumF (x -> x * x) a b 26. Anonymous Functions f x = e is the same as f = x -> e except that anonymous functions cant be recursive 27. Anonymous Functions Python lambda x1, x2, ...: e examples def sum_sqr(a, b): sum_func(lambda x: x * x, a, b) sorted(pairs, key=lambda p: p[0] / p[1]) 28. Anonymous Functions Python lambda x1, x2, ...: e examples def sum_sqr(a, b): sum_func(lambda x: x * x, a, b) sorted(pairs, key=lambda p: p[0] / p[1]) 29. Topics 1 Higher-Order Functions Function Order Example: Sorting Anonymous Functions Example: Fixed Points 2 List Functions Filter Map Fold Example: Edit Distance 30. Fixed Points x is a fixed point of f: f(x) = x repeatedly apply f until value doesnt change: x f(x) f(f(x)) f(f(f(x))) . . . 31. Fixed Points find fixed point fixedPoint :: (Float -> Float) -> Float -> Float fixedPoint f firstGuess = fpIter firstGuess where fpIter :: Float -> Float fpIter guess | isCloseEnough guess next = next | otherwise = fpIter next where next = f guess isCloseEnough :: Float -> Float -> Bool isCloseEnough x y = (abs (x-y) / x) < 0.001 32. Square Roots use fixed points to compute square roots y = x y y = x y = x/y fixed point of the function f(y) = x/y sqrt :: Float -> Float sqrt x = fixedPoint (y -> x / y) 1.0 doesnt converge (try with x = 2) average successive values (average damping) sqrt x = fixedPoint (y -> (y + x/y) / 2.0) 1.0 33. Square Roots use fixed points to compute square roots y = x y y = x y = x/y fixed point of the function f(y) = x/y sqrt :: Float -> Float sqrt x = fixedPoint (y -> x / y) 1.0 doesnt converge (try with x = 2) average successive values (average damping) sqrt x = fixedPoint (y -> (y + x/y) / 2.0) 1.0 34. Topics 1 Higher-Order Functions Function Order Example: Sorting Anonymous Functions Example: Fixed Points 2 List Functions Filter Map Fold Example: Edit Distance 35. Filter select all elements with a given property all odd elements of a list -- allOdds [4, 1, 3, 2] ~> [1, 3] allOdds :: [Integer] -> [Integer] -- primitive recursive allOdds [] = [] allOdds (x:xs) | odd x = x : allOdds xs | otherwise = allOdds xs -- list comprehension allOdds xs = [x | x [a] -> [a] 37. Filter filter: select elements that satisfy a predicate -- primitive recursive filter f [] = [] filter f (x:xs) | f x = x : filter f xs | otherwise = filter f xs -- list comprehension filter f xs = [x | x Bool) -> [a] -> [a] 38. Filter Example all odd elements of a list allOdds :: [Integer] -> [Integer] allOdds xs = filter odd xs Python def odd(n): return n%2 == 1 filter(odd, [4, 1, 3, 2]) 39. Filter Example how many elements in a list are above a threshold? howManyAbove :: Float -> [Float] -> Int howManyAbove t xs = length (filter (x -> x > t) xs) 40. Splitting Lists take elements from the front of a list while a predicate is true takeWhile even [8, 2, 4, 5, 6] ~> [8, 2, 4] takeWhile :: (a -> Bool) -> [a] -> [a] takeWhile f [] = [] takeWhile f (x:xs) | f x = x : takeWhile f xs | otherwise = [] exercise: drop elements from the front of a list while a predicate is true dropWhile even [8, 2, 4, 5, 6] ~> [5, 6] 41. Splitting Lists take elements from the front of a list while a predicate is true takeWhile even [8, 2, 4, 5, 6] ~> [8, 2, 4] takeWhile :: (a -> Bool) -> [a] -> [a] takeWhile f [] = [] takeWhile f (x:xs) | f x = x : takeWhile f xs | otherwise = [] exercise: drop elements from the front of a list while a predicate is true dropWhile even [8, 2, 4, 5, 6] ~> [5, 6] 42. Topics 1 Higher-Order Functions Function Order Example: Sorting Anonymous Functions Example: Fixed Points 2 List Functions Filter Map Fold Example: Edit Distance 43. Map transform all elements of a list example: floors of all elements of a list -- floorAll [5.7, 9.0, 2.3] ~> [5, 9, 2] floorAll :: [Float] -> [Integer] -- primitive recursive floorAll [] = [] floorAll (x:xs) = floor x : floorAll xs -- list comprehension floorAll xs = [floor x | x [a] -> [b] 45. Map map: apply a function to all elements of a list -- primitive recursive map f [] = [] map f (x:xs) = f x : map f xs -- list comprehension map f xs = [f x | x b) -> [a] -> [b] 46. Map Example floors of all elements of a list floorAll :: [Float] -> [Integer] floorAll xs = map floor xs Python import math map(math.floor, [5.7, 9.0, 2.3]) 47. Map Examples make a list of n copies of an item replicate :: Int -> a -> [a] replicate n i

Recommended

View more >