------------------------------------------------------------------------ -- DISTRIBUTION OF SCORES FOR CIS 252 QUIZ 2 -- range: scores -- 0- 1: -- 2- 3: -- 4- 5: 4 -- 6- 7: 7 -- 8- 9: 9 9 -- 10-11: 10 11 11 -- 12-13: 12 12 -- 14-15: 14 14 15 15 15 -- 16-17: 16 16 16 16 17 17 -- 18-19: 18 18 19 19 19 -- 20-21: 20 20 21 21 21 21 -- 22-23: 22 22 23 -- 24-25: -- average = 15.88 median = 16.5 ------------------------------------------------------------------------ -- ANSWERS ------------------------------------------------------------- ------------------------------------------------------------------------ ------------------------------------------------------------------------ -- Question 1. -- Suppose we have the following definitions: x, y :: Int x = 3 y = 9 lon :: [Int] lon = [ z+2 | z <-[1..5] ] nss :: [[Int]] nss = [ [23,98],[10,13,17],[7,-2] ] folie :: [a] -> Int folie [] = 1 folie (x:y:zs) = 2 folie (x:xs) = 3 -- Give the value of each of the following expressions. q1a = [4..y] -- Value: [4,5,6,7,8,9] q1b = lon -- Value: [3,4,5,6,7] q1c = (x>y,[x-y]) -- Value: (False,[-6]) q1d = [True]:[] -- Value: [[True]] q1e = folie [nss] -- Value: 3 -- Give the type of the following expression -- (e.g., the type of (head nss) is [Int]). q1f = [(odd y,div y 2) | y <- lon] -- Type: [(Bool,Int)] -- ------------------------------------------------------------------------ -- Question 2. -- Write a Haskell function zap :: Char -> String -> String -- such that (zap c str) returns the list obtained by removing -- every occurrence of the character c from the string str. -- (Remember, a String is just a list of Chars.) -- For example: -- (zap 'b' "abracadabra") returns "aracadara" -- (zap 'x' "abracadabra") returns "abracadabra" zap x ys = [y | y <- ys, x/=y] zap' :: (Eq a) => a -> [a] -> [a] zap' x [] = [] zap' x (y:ys) | x==y = zap' x ys | otherwise = y:zap' x ys ------------------------------------------------------------------------ -- Question 3. -- Write a Haskell function compress :: [Int] -> [Int] -- such that (compress xs) removes all successive duplicate entries in -- the list xs; non-successive duplicates are left alone. -- For example, the function should have the following behavior: -- (compress [1,1,1,10,8,8,1,8]) returns [1,10,8,1,8] -- (compress [5,5,5,5]) returns [5] compress [] = [] compress [x] = [x] compress (x:y:zs) | x==y = compress (y:zs) | otherwise = x:compress (y:zs) compress' [] = [] compress' [x] = [x] compress' (x:ys) | x==z = z:zs | otherwise = x:z:zs where (z:zs) = compress ys ------------------------------------------------------------------------ -- Question 4. -- Write a Haskell function nearest :: Float -> [Float] -> Float -- such that (nearest x nums) returns the number in nums -- that is closest in value to x. Break ties however you want. -- (E.g., (nearest 2.0 [1.0,3.0,99.6]) can return either -- 1.0 or 3.0.) Also, (nearest x []) returns 0.0. For example: -- (nearest 100.0 [2.56]) returns 2.56 -- (nearest 0.0 [-10.0, 10.0, 1.4]) returns 1.4 -- (nearest 7.0 [0.0, 5.0, 10.0, 15.0]) returns 5.0 -- (Note: (abs x) returns the absolute value of the number x. -- E.g., (abs (5.0 - 7.5)) returns 2.5.) nearest _ [] = 0.0 nearest x [y] = y nearest x (y:ys) | (abs (x-y)) < (abs (x-z)) = y | otherwise = z where z = nearest x ys nearest' :: Float -> [Float] -> Float nearest' _ [] = 0.0 nearest' _ [y] = y nearest' x (y:z:zs) | (abs (x-y)) <= (abs (x-z)) = nearest' x (y:zs) | otherwise = nearest' x (z:zs) nearest'' :: Float -> [Float] -> Float nearest'' _ [] = 0.0 nearest'' x ys | minBelow <= minAbove = minBelow-x | otherwise = minAbove+x where minBelow = minimum [x-y | y <- ys, y<= x] minAbove = minimum [y-x | y <- ys, y>= x]