CSCE 314 Programming Languages Homework Assignment 2 solution

$24.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (2 votes)

Problem 1. (5 points) Put your full name, UIN, and acknowledgements of any help received
in the head comment in your .hs file for this assignment.
Problem 2. (5 points) Chapter 4, Exercise 5. Using two nested conditional expressions in
the definition 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 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. (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.
1. (8 points) 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.
2. (7 points) 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. (15 points) Chapter 7. Exercise 9.
2
1. (10 points) Define altMap.
altMap :: (a -> b) -> (a -> b) -> [a] -> [b]
2. (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),
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).
Have fun!
3