COSC 341 HW 1017 SOLUTIONS "More Haskell Exercises" Question 1: 1. f a b = a ^ b 1. A. Show the blackbox and arrow description of the currying. 1. B. Explicitly give partially applied definitions for f 2 3 f a |-----------------| -----> | | | (f a) | b | |-----| | -------|-----> | | | | | |---|---> | | | | | ------- | |-----------------| Code -- following the example of *Learn You a Haskell*, pp 60-61 f a b = a ^ b For the function call f 2 3, the implicitly defined (curried) function is f2 = f 2 Copy of GHCI interpretation: *Main> f 2 3 8 *Main> f2 3 8 /****************************************************************************/ Question 2: 2. g a b c = a ^ b / c 2. A. Show the blackbox and arrow description of the currying. 2. B. Explicitly give partially applied definitions for g 2 3 4 g a |---------------------------------| -----> | | | (g a) | b | |-------------------| | -------|-----> | | | | | | | | | (g a b) | | c | | |---------| | | -------|-------|---->| | | | | | | |---|-----|----> | | |---------| | | | | | | | |-------------------| | |---------------------------------| Code -- again, following the example of *Learn you a Haskell*, pp 60 - 61: g a b c = a ^ b / c g2' = g 2 g23' = g2' 3 -- AKA ( (g 2) 3 ) Copy of GHCI interpretation: *Main> g 2 3 4 2.0 *Main> g2' 3 4 2.0 *Main> g23' 4 2.0 /***************************************************************************/ Question 3: 3. disc a b c = b ^ 2 - 4 * a * c 3. A. Show the blackbox and arrow description of the currying. 3. B. Explicitly give partially applied definitions for disc 1 2 1 disc a |---------------------------------| -----> | | | (disc a) | b | |-------------------| | -------|-----> | | | | | | | | | (disc a b) | | c | | |---------| | | -------|-------|---->| | | | | | | |---|-----|----> | | |---------| | | | | | | | |-------------------| | |---------------------------------| Code: following the example of *Learn you a Haskell* pp 60-61. disc a b c = (b ^ 2) - (4 * a * c) disc1 = disc 1 disc12 = disc1 2 Copy of GHCI interpretation: *Main> disc 1 2 1 0 *Main> disc1 2 1 0 *Main> disc12 1 0 Redo-ing 3B with different parameters, producing explicit code in the manner of *Learn you a Haskell*, pp 60-61. disc 2 3 4 disc a b c = (b ^ 2) - (4 * a * c) disc2 = disc 2 disc23 = disc2 3 Copy of GHCI interpretation: *Main> disc 2 3 4 -23 *Main> disc2 3 4 -23 *Main> disc23 4 -23 /**************************************************************************/ Question 4: 4. A. Give a function that uses list comprehension to implement the map function. Your function is named map' 4. B. Demonstrate your map' works on map' (3:) [ [], [2, 4], [1, 2, 3, 4] ] map' (++ "!") ["abc", "XYZ"] map' fun li = [ fun x | x <- li ] GHCI interpreter: *Main> map' (3:) [ [], [2, 3], [1, 2, 3, 4] ] [[3],[3,2,3],[3,1,2,3,4]] *Main> map' (++ "!") ["abc", "XYZ"] ["abc!","XYZ!"] *Main> /**************************************************************************/ Question 5: 5. A. Give a function using list comprehension and predicates to implement the filter function. Your function is named filter' 5. B. Demonstrate your filter' works on filter' (>3) [4, 2, 5, 1, 7] filter' (`elem` ['a'..'z']) "What Planet Is THIS?" CODE: filter' pred li = [ x | x <- li, (pred x) == True ] GHCI interpreter: *Main> filter' (>3) [4, 2, 5, 1, 7] [4,5,7] *Main> filter' (`elem` ['a'..'z']) "What Planet Is THIS?" "hatlanets" *Main>