Presentation is loading. Please wait.

Presentation is loading. Please wait.

GC16/3011 Functional Programming Lecture 9 Higher Order Functions

Similar presentations


Presentation on theme: "GC16/3011 Functional Programming Lecture 9 Higher Order Functions"— Presentation transcript:

1 GC16/3011 Functional Programming Lecture 9 Higher Order Functions

2 Contents Higher Order Functions Combinators
Definition Example: composition Combinators Example: S, K, I Capturing common forms of recursion on lists Examples: map and filter

3 Higher Order Functions
Definition: A Higher Order Function is a function that either (i) takes a function as an argument, or (ii) returns a function as a result, or (iii) both. f g x = (g x) h x = (+ x) j f p g = (+ (f p)), if p = (+ (g p)), otherwise

4 Higher Order Functions: types
f g x = (g x) h :: num -> (num -> num) h x = (+ x) j :: (bool-> num) -> bool -> (bool -> num) -> (num -> num) j f p g = (+ (f p)), if p = (+ (g p)), otherwise

5 Function composition Can partially apply “compose”:
compose f g x = f (g x) Can partially apply “compose”: fred = (compose (+ 1) abs) main = fred (-3) Built-in operator “.” fred = ((+1) . abs)

6 Function composition (2)
twice x = x * 2 many x = twice (twice (twice (twice x))) mymany :: num -> num mymany = (twice . twice . twice . twice)

7 Combinators Definition Examples:
A combinator is a function that contains no free variables Examples: fst (x,y) = x id x = x || “I” cancel x y = x || “K” swap f x y = f y x || “C” distribute f g x = f x (g x) || “S”

8 Combinators (2) Basis for implementation
S & K computationally complete All required data available via arguments (passed on the stack) so no need to search for distant bindings

9 Example HOF “myiterate” repeatedly applies its second parameter to its final parameter; the final parameter is an accumulator for the result. The function loops n times, where n is the first parameter: myiterate :: num -> (* -> *) -> * -> * myiterate 0 f state = state myiterate n f state = myiterate (n-1) f (f state)

10 Use of example HOF printdots n = myiterate n ((++) “.”) “” printdots 3
((++) “.” ((++) “.” ((++) “.” “”))) “…”

11 Recursion on lists: capturing common forms
Mapping a function across the values of a list: notlist [] = [] notlist (x:xs) = ((~ x) : (notlist xs)) inclist [] = [] inclist (x:xs) = ((x+1) : (inclist xs)) abslist [] = [] abslist (x:xs) = ((abs x) : (abslist xs))

12 Recursion on lists: map
Built-in function “map” makes life easier: notlist any = map (~) any inclist any = map (+1) any abslist any = map abs any Or, simply: notlist = map (~) inclist = map (+1) abslist = map abs

13 Definition of map map f [] = [] map f (x:xs) = ((f x): (map f xs))
So, what’s the type of map? Write it here:

14 Recursion on lists: capturing common forms
Filtering some elements out of a list: firsts [] = [] firsts (x:xs) = (x : (firsts xs)), if (x>=70) = firsts xs, otherwise not [] = [] not34 (x:xs) = (x : (not34 xs)), if (x~=34) = not34 xs, otherwise

15 Recursion on lists: filter
Built-in function “filter” makes life easier: firsts any = filter (>=70) any not34 any = filter (~=34) any Or, simply: firsts = filter (>=70) not34 = filter (~=34)

16 Definition of filter filter f [] = []
filter f (x:xs) = (x: (filter f xs)), if (f x) = filter f xs, otherwise So, what’s the type of filter? Write it here:

17 Challenge Can you write a function that takes a list of numbers (containing only the values 1, 2 and 0, where at least one 0 must occur) and returns a three-tuple containing: The number of 1s before the first 0 The number of 2s before the first 0 The length of the longest sequence of 1s before the first 0 If you find that easy, try designing the program a different way so that it makes use of higher order functions (e.g. filter, dropwhile, takewhile)

18 Summary Higher Order Functions Combinators Use of example HOF
Definition Example: composition Combinators Example: S, K, I Use of example HOF Capturing common forms of recursion on lists Examples: map and filter


Download ppt "GC16/3011 Functional Programming Lecture 9 Higher Order Functions"

Similar presentations


Ads by Google