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) 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) 7) 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. 8) (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. 9) 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. 10) 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 8 so that it uses threeEqual. 11) 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] ] 12) Use the Hug's :type command to find out the type for each of the following expressions. Explain the results. [] []:[] []:[]:[] []:[]:[]:[] ([]:[]):[] 13) Write an expression, using only the cons operator and the empty list (as in Exercise 12), 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. 14) 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? 15) 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) 16) Write a function called exercise16 that has the following type. exercise16 :: (a,b) -> (c,a) -> [a] Make your definition of exercise16 as simple as you can, but be sure to use Hug's :type command to check the type of your definition. 17) Write a function called exercise17 that has the following type. exercise17 :: (a,b) -> (b,a) -> ([a],[b]) Make your definition of exercise17 as simple as you can, but be sure to use Hug's :type command to check the type of your definition. 18) Consider the following function. ex18 :: (Int -> Int) -> Int -> Int ex18 f x = f x Explain this function's type and its definition. Give several (error free) examples of applying this function. 19) 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?" 20) 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. 21) Figure out what this function does. ex21 x = [y | y <- x, y /= ' '] 22) 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" 23) Use a list comprehension to count to 256 in hexadecimal.