Recursion and Induction: Higher Order Functions Greg Plaxton - - PowerPoint PPT Presentation
Recursion and Induction: Higher Order Functions Greg Plaxton - - PowerPoint PPT Presentation
Recursion and Induction: Higher Order Functions Greg Plaxton Theory in Programming Practice, Spring 2005 Department of Computer Science University of Texas at Austin Higher Order Functions A higher order function is a function that takes a
Higher Order Functions
- A higher order function is a function that takes a function as an
argument
- In this lecture we will see a number of examples of useful higher order
functions
- We will see that some of the functions we defined in previous lectures
are more easily defined using a suitable higher order function
Theory in Programming Practice, Plaxton, Spring 2005
Remarks on Infix and Prefix Operators
- We use the term operator to mean a function of two arguments
- An infix operator, such as +, is written between its two arguments,
whereas a prefix operator, such as max, precedes its arguments
- As we have remarked in an earlier lecture, an operator can be converted
from prefix to infix by surrounding it with backquotes – Example: div 5 3 is the same as 5 ‘div‘ 3
- An operator can be converted from infix to prefix by parenthesizing it
– Example: (+) x y is the same as x + y
Theory in Programming Practice, Plaxton, Spring 2005
An Example of a Higher Order Function
- We previously developed the functions suml and multl that operate
similarly on the argument list – For the empty list, each function produces a specific value (0 for suml, 1 for multl) – For a nonempty list, say x:xs, the item x and the function value for xs are combined using a specific operator (+ for suml, * for multl)
- This suggests that we can code a generic function that has three
arguments – The specific value to be returned for the empty list – The operator to be used for combining – The list on which the function is to be applied
Theory in Programming Practice, Plaxton, Spring 2005
Function foldr
- Here is the definition of function foldr
foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs)
- Various functions can be easily defined using foldr
suml xs = foldr (+) xs multl xs = foldr (*) 1 xs andl xs = foldr (&&) True xs
- rl
xs = foldr (||) False xs eql xs = foldr (==) True xs flatten xs = foldr (++) [] xs
Theory in Programming Practice, Plaxton, Spring 2005
Remark
- There is an even nicer way to define functions such as suml and multl;
just omit xs from both sides of the function definition suml = foldr (+) multl = foldr (*) 1 andl = foldr (&&) True
- rl
= foldr (||) False eql = foldr (==) True flatten = foldr (++) []
- In this course, we will not describe the justification for this type of
definition
Theory in Programming Practice, Plaxton, Spring 2005
The Type of Function foldr
- Let’s ask hugs to tell us the type of function foldr
Main> :t foldr foldr :: (a -> b -> b) -> b -> [a] -> b
- In all of the examples given previously, the types a and b were the same
(e.g., the operator (+) takes two arguments of the same type)
- In the next slide we will give an example in which types a and b differ
Theory in Programming Practice, Plaxton, Spring 2005
Another foldr Example
- The function ev takes an integer and a boolean as arguments and
returns a boolean ev x b = (even x) && b
- The function evenl determines if all integers of a given list are even
evenl = foldr ev True
Theory in Programming Practice, Plaxton, Spring 2005
Function fold
- Function fold is a simpler version of foldr that applies to nonempty
lists only fold f [x] = x fold f (x:xs) = f x (fold f xs)
- We now define the function maxl in terms of fold
maxl = fold max
Theory in Programming Practice, Plaxton, Spring 2005
Function map
- Another useful higher order function is map
– The arguments are a function f and a list of elements on which f can be applied – The return value is the list obtained by applying f to each element
- f the given list
- Here is the definition of map
map f [] = [] map f (x:xs) = (f x) : (map f xs)
Theory in Programming Practice, Plaxton, Spring 2005
Function map
- Here are some examples involving map
Main> map not [True,False] [False,True] Main> map even [2,4,5] [True,True,False] Main> map chCase "jmisra" "JMISRA" Main> map len ["Jayadev","Misra"] [7,5]
- What is the type of map?
- Characterize the value of the following expression
andl (map even xs)
Theory in Programming Practice, Plaxton, Spring 2005
Function filter
- Another useful higher order function is filter
– The arguments are a predicate p and a list xs – The return value is the list containing the elements of xs for which p holds
- Here is the definition of filter
filter p [] = [] filter p (x:xs) | p x = x: (filter p xs) | otherwise = (filter p xs)
Theory in Programming Practice, Plaxton, Spring 2005
The Function filter
- Here are some examples involving filter
Main> filter even [2,3,4] [2,4] Main> filter digit [’a’,’9’,’b’,’0’,’c’] "90" Main> filter upper "Jayadev Misra" "JM" Main> filter digit "Jayadev Misra" ""
- What is the type of filter?
Theory in Programming Practice, Plaxton, Spring 2005