## Description

Below, the exercise problems are from the Haskell Textbook: \Programming in Haskell, 2nd Ed.”, by Graham Hutton. Some problems are modi ed (with additional requirements) by the instructor. Please read corresponding textbook chapters and the problem statements carefully, paying attention to the requirements. For example, \using foldr, de ne . . . ” means that using the foldr function when you de ne the assigned function is required. There may be signi cant penalties for not ful lling such requirements. Keep the name and type of each function exactly the same as given in the problem statement and the skeleton code.

Problem 1. (5 points) Put your full name, UIN, and acknowledgements of any help received in the head comment in your .hs le for this assignment.

Problem 2. (5 points) Chapter 4, Exercise 5. Using two nested conditional expressions in the de nition is a requirement.

Problem 3. (20 points) Chapter 4, Exercise 8.

Problem 4. (10 points) Chapter 5, Exercise 6. Using a list comprehension and factors in the de nition is a requirement. Include the de nition of factors in your hw2.hs le (the de nition is in the text as well as in my lecture slides).

Problem 5. (7 + 7 + 6 = 20 points) Chapter 6, Exercise 5. Your answer should follow the style of examples such as reverse, (++), insert, and zip in pages 62{64 in the text. Write your answer neatly and clearly within a block comment {- -}.

Problem 6. (15 points) This problem has two subproblems. In Assignment 1, Problems 5 and 6, you implemented merge sort that sorts a list in an ascending order.

- (8 points) De ne a recursive function mergeBy that merges two sorted lists by the given criterion, for example, in an ascending order or in a descending order (so that the resulting list is also sorted). The type signature of mergeBy is as follows.

mergeBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]

Notice the di erence from merge :: Ord a => [a] -> [a] -> [a] in Ch. 6 Exer-cise 7 such that mergeBy accepts three arguments, the rst of which is a comparison function of type (a -> a -> Bool) that determines in which way the list is to be sorted. Such comparison function that returns a Boolean value (true or false) is called a predicate.

- (7 points) Using mergeBy that you wrote above and halve that you wrote for Prob-lem 6 in Assignment 1, de ne a recursive function msortBy. The problem speci cation stays the same as that for msort in Ch. 6 Exercise 8, except the additional requirement of the rst argument being a predicate. Thus, the type of msortBy is:

msortBy :: (a -> a -> Bool) -> [a] -> [a]

Problem 7. (15 points) Chapter 7. Exercise 9.

- (10 points) De ne altMap.

altMap :: (a -> b) -> (a -> b) -> [a] -> [b]

- (5 points) Explain how your altMap works when it is applied as below.

> altMap (*2) (‘div‘ 2) [0..6]

Problem 8. (10 points) Using map, filter, and (.) (function composition operator), de ne a function that examines a list of strings, keeping only those whose length is odd, converts them to upper case letters, and concatenates the results to produce a single string.

concatenateAndUpcaseOddLengthStrings :: [String] -> String

You need to import Data.Char in order to use the toUpper function (see the skeleton code).

Have fun!

3