1.) Notice that the following expression evaluates to 1. Hugs> head (head [[1,2],[3,4]]) 1 Determine what compositions of the head and tail functions applied to the list [[1,2],[3,4]] evaluate to each of 2, 3, and 4. 2.) Explain why these evaluate the way they do. Hugs> (+) (head [5,4,3]) (head (tail [3,4,5])) 9 Hugs> head [(+), (-)] 2 3 5 Hugs> tail "dog" "og" Hugs> tail (head (tail [['c','a','t'], ['d','o','g']])) "og" 3.) Explain why these evaluate the way they do. Hugs> head [(+), (-), (*)] 2 3 5 Hugs> head [(+), (-), (*), (/)] 2 3 5.0 Hint: Use Hug's type command, :t, to ask about the type of each operator used in the expression and then check the type of each of the two lists. 4.) Simplify the arithmetic expression so that it still uses the variables x,y,r,s,t, but it uses only the infix version of each operator. (+) x ((*) y (r - (*) s t)) where x=4; y=5; r=2; s=3; t=6 5.) Simplify the arithmetic expression so that it still uses the variables x,y,z, but it uses only the infix version of each operator. (+2) ((*) ((*3) x) (y - ((+1) z))) where x=4; y=5; z=6 6.) Rewrite each of these list comprehensions in a different form. (a) [ x^2 | x <- [1..20], even x ] (b) [ 3*x | x <- [2, 4..20] ] (c) [ x+y | x <- [0, 6..20], y <- [0, 3] ] (d) [ x+y | x <- [0, 3..10], y <- [1..3] ] (e) [(x,y)| x <- [1..4], y <- [4, 3..1] ] (f) [(x,y)| x <- [1..5], y <- [1..5], y<=x ] (g) [(x,y)| x <- [1..5], y <- [0..6], abs(x-y)<2 ] (h) [(x,y)| x <- [1..4], y <- [x, x-1..0] ] 7.) Using a list comprehension, write a function that takes a list of Int values and an Int value n and returns those elements in the list that are greater than n. 8.) Figure out what this function does. ex8 x = [y | y <- x, y /= ' '] 9.) Carefully explain why these list comprehensions produce what they do. How would you produce similar output using Java? [ [d4,d3,d2,d1,d0] | d4 <- [0,1], d3 <- [0,1], d2 <- [0,1], d1 <- [0,1], d0 <- [0,1] ] [ [d4,d3,d2,d1,d0] | d4<-['0','1'], d3<-['0','1'], d2<-['0','1'], d1<-['0','1'], d0<-['0','1'] ] [ [d4,d3,d2,d1,d0] | d4 <- "01", d3 <- "01", d2 <- "01", d1 <- "01", d0 <- "01" ] [ [d4,d3,d2,d1,d0] | d4 <- d, d3 <- d, d2 <- d, d1 <- d, d0 <- d ] where d = "01" 10.) Use a list comprehension to count to 256 in hexadecimal. 11.) Suppose that f and g have the following types. f :: Int -> Int g :: Int -> Int -> Int Which of the following definitions make sense? Explain why. h1 x y = f g x y h2 x y = f (g x y) h3 x y = f (g x) y h4 x y = g (f x) y h5 x y = g f x y h6 x y = g (f x y) h7 x y = g x (f y) h8 x y = (g x) (f y) 12.) (a) Define a function shorter that takes two lists and returns true if the length of the first list is less than the length of the second list. Use Hug's type command to look at the type given to your shorter function. Explain the type. (b) Redefine shorter so that it takes two lists and returns the shorter of the two lists (if the lists have equal length, return the first one). Use Hug's type command to look at the type given to this version of shorter. Explain the type. How (and why) is it different from part (a)? 13.) (a) What does this function do? mystery :: Int -> Int -> Int -> Bool mystery x y z = not ((x==y) && (x==z)) (b) Use DeMorgan's Law to rewrite the mystery function. 14.) The following function threeEqual returns true only if all three of its arguments are the same number. threeEqual :: Int -> Int -> Int -> Bool threeEqual x y z = (x == y) && (x == z) (a) Give a definition for a function fourEqual that is modeled on the definition of threeEqual. (b) Redefine fourEqual so that is uses the function threeEqual. (c) Redefine the function mystery from Exercise 13 so that it uses threeEqual. 15.) Write a definition for the function threeDifferent that has this type threeDifferent :: Int -> Int -> Int -> Bool and so that the result of threeDifferent is true only if all three of its arguments are different numbers. Be sure to test your definition. 16.) Write a function which returns the head and the tail of a list as the first and second elements of a tuple. What would be this function's type? 17.) Write a function called ex17 that has the following type. ex17 :: (a,b) -> (c,a) -> [a] Make your definition of ex17 as simple as you can, but be sure to use Hug's :type command to check the type of your definition. 18.) Write a function called ex18 that has the following type. ex18 :: (a,b) -> (b,a) -> ([a],[b]) Make your definition of ex18 as simple as you can, but be sure to use Hug's :type command to check the type of your definition. 19.) Use the Hug's :type command to find out the type for each of the following expressions. Explain the results. [] []:[] []:[]:[] []:[]:[]:[] ([]:[]):[] 20.) Write an expression, using only the cons operator and the empty list (as in Exercise 19), that has the following type. ??? :: [[[[a]]]] In addition, give an example of a list that has the above type and contains two elements. Be sure to check your list with the :type command to make sure it has the correct type and check your list with the length function to make sure it has the correct length. 21.) Figure out for yourself, and then verify, the types of the following expressions, if they have a type. If they do not have a type, explain why. ’h’:’e’:’l’:’l’:’o’:[] "hello" [5, ’a’] (5, ’a’) 5 + 10 (5::Int) + 10 5 + (10::Double) (5::Int) + (10::Double) 22.) Figure out for yourself, and then verify, the types of the following expressions, if they have a type. If they do not have a type, explain why. f1 x y z = (x, y : z : []) f2 x = x 'a' f3 x = x (0::Int) f4 x = x x f5 x = "what?" 23.) Consider the following function. ex23 :: (Int -> Int) -> Int -> Int ex23 f x = f x Explain this function's type and its definition. Give several (error free) examples of applying this function. 24.) Use a list comprehension to define a function heads :: [[a]] -> [a] that consumes a list of lists and returns a list of the first element from each of the nested input lists. heads [[3,2,1],[1,2,3],[7],[5,6]] ==> [3,1,7,5] 25.) Use a list comprehension to define a function tails :: [[a]] -> [[a]] that consumes a list of lists and returns a list of the tail list from each of the nested input lists. tails [[3,2,1],[1,2,3],[7],[5,6]] ==> [[2,1],[2,3],[],[6]]