module HW2revised where import Prelude {- For each of the following questions, put your answer directly below the question. This homework is due on Wednesday, September 23rd, by 3pm. Email your code directly to Adam Procter: amp269@mizzou.edu. (1) You must use a text editor (e.g., vi, textpad, emacs, etc.) to prepare your solution. (2) You must write type declarations for each and every one of your Haskell definitions. (3) The program you turn in must be the product of your effort alone. A note about error messages: one function you may use to report a fatal error is the built-in function: error :: String -> a Ex: Hugs> error "arghh!" Program error: arghh! -} {- Part A. Patterns. When we've written functions that take lists as input, they have always had the form: foo :: [a] -> ... foo [] = ... foo (x:xs) = ... The patterns here are "[]" and "(x:xs)". In fact, one can use other patterns as well. The pattern "(x:[])" matches a list with one element, the pattern "(x:y:[])" matches a list with exactly two elements, the pattern "(x:y:z:xs)" matches a list with at least three elements. Keeping this in mind, write the following function using pattern matching. 1. Write and test the function lastitem, which returns the final (last) element of a list, using a recursive definition. Here is how lastitem behaves: Hugs> lastitem [1,2,3,4,5,6] 6 Hugs> lastitem [] Error Message -} {- Part A.2: General hacking. 2. Write and test the function partition, which splits a list into two equal parts, or fails for even length lists. Hugs> partition [1,2,3,4,5,6] ([1,2,3],[4,5,6]) Hugs> partition [1] Error Message REVISION (9/18): You MAY use these built-in functions in your answer to problem 2: length :: [a] -> Int take :: Int -> [a] -> [a] drop :: Int -> [a] -> [a] -} {- Part B: Lambda expressions and map. Recall from Chapter 4 of Hutton, that we can define a function odds that returns a list of consecutive odd numbers: odds :: Int -> [Int] odds n = map (\ i -> 2*i + 1) [0..n-1] Notice the use of both map and lambda expressions to accomplish the task. For the next two problems, use map and a lambda expression to compute the following Hugs> period ["No way","More peas","Walt Whitman"] ["No way.","More peas.","Walt Whitman."] :: [String] Hugs> divby 10 [1,2,3] [10,5,3] :: [Int] Hugs> divby 10 [1,2,0] [10,5, Program error: divide by zero You may use the concatenation function (++) in your definition of period (look it up in Hutton). -} -- 3. -- period :: ? -- 4. Just change the definition below: divby :: Int -> [Int] -> [Int] divby = undefined {- Part C: Using Maybe. 5. Write a safe version of divby, sdivby :: Int -> [Int] -> [Maybe Int] Your answer should involve only a simple alteration of your answer to the previous problem. It will behave as: Hugs> sdivby 10 [1,2,0] [Just 10,Just 5,Nothing] Hugs> sdivby 10 [1,2,3] [Just 10,Just 5,Just 3] -} {- 6. The following functions are written in curried form. Convert the code into uncurried form. Remember to provide type declarations for both as well. -} beavis n _ | n <= 0 = [] beavis _ [] = [] beavis n (x:xs) = x : beavis (n-1) xs butthead n xs | n <= 0 = xs butthead _ [] = [] butthead n (_:xs) = butthead (n-1) xs