These are exercises about "higher order functions". 1) Find the value of each of these expressions. a) (\x -> 3*x+2) 5 b) (\x -> (\y -> 3*x+y)) 5 2 c) (\x -> x+2) ((\x -> 3*x) 5) d) (\x -> (\y -> x (x y))) (\w -> 3*w) 4 e) (\x -> (\y -> x*y)) ((\w -> 3*w) 4) 5 f) (\x -> (\y z -> z (x+y))) 3 4 (\w -> 5*w) g) (\f g x -> f (g x)) (\x -> 5*x) (\x -> 6*x) 2 2) Let f x y z = 2*x + 3*y + 4*z Describe each of the functions g1 through g14 (what formula does each function simplify to?). g1 = f 5 g2 u v = f 5 u v g3 u v = f 5 v u g4 u v = f u 5 v g5 u v = f u v 5 g6 = f 5 4 g7 u = f 5 4 u g8 u = f u 5 4 g9 u = f 5 u 4 g10 = g2 5 g11 = g3 5 g12 = g4 5 g13 = g5 5 g14 u v = g3 v u 3) Compute the result of each map. a) map length [[2,3,5],[4],[6,7,4,3],[4,4]] b) map head [[2,3,5],[4],[6,7,4,3],[4,4]] c) map tail [[2,3,5],[4],[6,7,4,3],[4,4]] d) map (map (+3)) [[2,3,5],[4],[6,7,4,3],[4,4]] e) map (map (\x->x*x)) [[2,3,5],[4],[6,7,4,3],[4,4]] 4) Compute the result of each foldr. a) foldr (+) 10 [1,2,3,4,5] b) foldr1 (+) [1,2,3,4,5] c) foldr (\x y -> if even x then x+y else y) 0 [1,2,3,4,5,6] d) foldr (\x y -> (head x):y) [] ["bye","saw","we","cat","dog"] e) foldr map [2,3,4,5,6,7] [\x->2*x, \x->3*x, \x->x*x] For each of the exercises 5 through 9, use the map function to write one line of code that solves the problem. Your one line of code can use an anonymous lambda expression. 5) Write a one line function multpairs :: [(Int,Int)] -> [Int] that consumes a list of pairs of integers and returns a list of the products of each pair. 6) Write a one line function incList :: Num a => a -> [a] -> [a] that increments each element of the input list by numeric parameter. 7) Write a one line function pairList :: [a] -> [(a,a)] that puts each element from the input list into a pair that duplicates that element. Here is an example. pairList [1,2,3,4] ==> [(1,1),(2,2),(3,3),(4,4)] 8) Write a one line function positives :: [Integer] -> [Integer] that copies each number from the input list to the output list unless the number is negative in which case the input number is replaced by zero in the output list. 9) Write a one line function maxPairs :: [(Integer,Integer)] -> [Integer] which consumes a list of pairs and returns the list of the maximum elements from each pair in the input list. For each of the exercises 10 through 16, use the foldr function to write one line of code that solves the problem. Your one line of code can use an anonymous lambda expression. 10) Write a one line function dupList :: [a] -> [a] which duplicates each element of the input list twice in a row in the output list. Here is an example. dupList [1,2,3,4] ==> [1,1,2,2,3,3,4,4] 11) Write a one line function trueCount :: [Bool] -> Int that consumes a list of boolean values and returns the number of True values in the input list. 12) Write a one line function sumMaxs :: [(Integer,Integer)] -> Integer which consumes a list of pairs of integers and returns the sum of the maximum elements from each pair. Here is an example. sumMaxs [(1,2),(-1,1),(3,0),(4,4),(5,4)] ==> 15 13) Write a one line function max :: Ord a => [a] -> a that returns the largest element in the non-empty input list. 14) Write a one line function member :: Ord a => a -> [a] -> Bool that returns true if and only if the first parameter is an element of the list parameter. 15) Write a one line function less :: Ord a => a -> [a] -> [a] that returns a list of all the elements from the input list that are less than the first input parameter. 16) Write a one line function addPairsPointwise :: Num a => [(a,a)] -> (a,a) that consumes a list of pairs of numbers and produces a pair of numbers where the first number in the output pair is the sum of all the first numbers from the input pairs and the second number in the output pair is the sum of all the second numbers from the input pairs. Here is an example. addPairsPointwise [(1,2),(3,4),(5,6),(7,8)] ==> (16,20) 17) Write a recursive function indexPairs :: [a] -> [(Integer,a)] that consumes a list and produces a list of pairs where every element from the input list is paired up with its index from the input list. Here are two examples. indexPairs "hello" ==> [(0,'h'),(1,'e'),(2,'l'),(3,'l'),(4,'o')] indexPairs (take 4 (repeat 5)) ==> [(0,5),(1,5),(2,5),(3,5)] Notice that this function consumes a list and produces another list the same length as the input list. Quite often this is a sign that the function can be written using the map function. Explain why indexPairs is NOT a good candidate for being implemented using map. Try to implement indexPairs using foldr or foldl. 18) (a) Write a function squarecube :: Num a => [a] -> [(a,a)] that consumes a list of numbers and produces a list of pairs of numbers where the first number in a pair is the square of the corresponding input number, and the second number in a pair is the cube of the corresponding input number. Here is an example. squarecube [1,2,3,4] ==> [(1,1),(4,8),(9,27),(16,64)] (b) Write a function squarecube' :: Num a => [(a,a)] -> [(a,a)] that consumes a list of pairs of numbers and produces a list of pairs of numbers where the first number in an output pair is the square of the first number from the corresponding input pair, and the second number in an output pair is the cube of the second number from the corresponding input pair. Here is an example. squarecube' [(1,2),(2,3),(3,4)] ==> [(1,8),(4,27),(9,64)] 19) The result of evaluating this expression head (tail (map map [(3+), (3-), (3*)])) [0,1,2,3] is the following list. [3,2,1,0] Explain why. 20) Explain how Haskell evaluates the following expression. foldr map [[2],[3],[4],[5],[6],[7]] (map map [(3+), (3-), (3*)])