Problem 1. Put your full name, UIN, and acknowledgements of any help received in the head comment in your .hs file for this assignment.
Problem 2. Chapter 4, Exercise 5. Using two nested conditional expressions in the definition is a requirement.
Problem 3. Chapter 4, Exercise 8.
Problem 4. Chapter 5, Exercise 6. Using a list comprehension and factors in the definition is a requirement. Include the definition of factors in your hw2.hs file (the definition is in the text as well as in my lecture slides).
Problem 5. Chapter 6, Exercise 5. Your answer should follow the style of examples such as reverse, (++), insert, and zip in pages 6264 in the text. Write your answer neatly and clearly within a block comment {- -}.
Problem 6. This problem has two subproblems. In Assignment 1, Problems 5 and 6, you implemented merge sort that sorts a list in an ascending order.
- Define 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 difference from merge :: Ord a => [a] -> [a] -> [a] in Ch. 6 Exercise 7 such that mergeBy accepts three arguments, the first 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.
- Using mergeBy that you wrote above and halve that you wrote for Problem 6 in Assignment 1, define a recursive function msortBy. The problem specification stays the same as that for msort in Ch. 6 Exercise 8, except the additional requirement of the first argument being a predicate. Thus, the type of msortBy is:
msortBy :: (a -> a -> Bool) -> [a] -> [a]
Problem 7. Chapter 7. Exercise 9.
- Define altMap.
altMap :: (a -> b) -> (a -> b) -> [a] -> [b]
- Explain how your altMap works when it is applied as below.
> altMap (*2) (div 2) [0..6]
Problem 8. Using map, filter, and (.) (function composition operator), define 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).
Reviews
There are no reviews yet.