[Solved] CSCE314 Homework 1-Programming in Haskell

$25

File Name: CSCE314_Homework_1-Programming_in_Haskell.zip
File Size: 386.22 KB

SKU: [Solved] CSCE314 Homework 1-“Programming in Haskell Category: Tag:
5/5 - (1 vote)

Below, the exercise problems are from the Haskell Textbook: Programming in Haskell, 2nd Ed. by Graham Hutton. Some problems are modified (with additional requirements) by the instructor. Please read textbook Chapters 1 thru 6 and the problem statements carefully. Keep the name and type of each function exactly the same as given in the problem statement and the skeleton code.

Problem 1. Put your full name, UIN, and acknowledgements of any help received in the head comment in your .hs file.

Problem 2. The n-th Lucas number `n is recursively defined as follows: `0 = 2 and `1 = 1 and `n = `n1+`n2 for n 2. Write a recursive function lucas that computes the n-th Lucas number. lucas :: Int -> Int

Problem 3. Chapter 1, Exercise 4 (modified).

Study the definition of the function qsort given in the text carefully, and try it out with an integer list, for example, [3,2,3,1,4]. You will notice that it sorts a list of elements in an ascending order.

3.1 (5 points) Write a recursive function qsort1 that sorts a list of elements in a descending order.

qsort1 :: Ord a => [a] -> [a]

3.2 (10 points) Write your answer for this question in a block comment following the definition of function qsort1. Suppose that qsort1 is invoked with the input [3,2,3,1,4]. How many times is qsort1 called recursively (i.e., without counting the first invocation of qsort1 [3,2,3,1,4])? Explain step-by-step, in particular, at each level of recursive call, what are the values of x, smaller, and larger? [Hint: Think using a binary tree structure.]

Problem 4. Chapter 5, Exercise 9 (modified).

The scalar product function should be able to take not only two lists of the same length n but also those of different lengths.

4.1 Using the list comprehension with the zip prelude function, write the scalar product function.

scalarproduct :: [Int] -> [Int] -> Int

4.2 Explain carefully step-by-step the working of your scalarproduct function when it is invoked as follows.

*Main> scalarproduct [1,2,3] [4..]

Problem 5. () Chapter 6, Exercise 7.

Like in the previous problem, the merge function should be able to take two sorted lists of different lengths as well as those of the same length.

merge :: Ord a => [a] -> [a] -> [a]

Problem 6. Chapter 6, Exercise 8.

Defining function halve is required (hint: use splitAt prelude function).

(8 points) msort :: Ord a => [a] -> [a] (7 points) halve :: [a] -> ([a], [a])

Problem 7. Write a recursive function isElem that returns True if a given value is an element of a list or False otherwise.

isElem :: Eq a => a -> [a] -> Bool

Sets are the most fundamental discrete structure on which all other discrete structures are built. In the following problem (and continuing in the next homework set), we are going to implement mathematical sets and their operations using Haskell lists.

A set is an unordered collection of elements (objects) without duplicates, whereas a list is an ordered collection of elements in which multiplicity of the same element is allowed. We define Set as a type synonym for lists as follows: type Set a = [a]

Even though the two types Set a and [a] are the same to the Haskell compiler, they communicate to the programmer that values of the former are sets and those of the latter are lists.

Problem 8. Write a recursive function that constructs a set from a list. Constructing a set from a list simply means removing all duplicate values. Use isElem from the previous problem in the definition of toSet.

toSet :: Eq a => [a] -> Set a

Skeleton code and modes of running your code: The file hw1-skeleton.hs contains stubs for all the functions you are going to implement and placeholders for your explanations. The Haskell function bodies are initially undefined, a special Haskell value that has all possible types (thus, the skeleton file at least compiles).

In the skeleton file you find a test suite that test-evaluates the functions. Initially, all tests fail, until you provide correct implementation for the Haskell function. The tests are written using the HUnit library. Feel free to add more tests to the test suite.

The skeleton code can be loaded to the interpreter (> ghci hw1-skeleton.hs). In the interpreter mode, you can test individual functions one at a time while you are implementing them. Evaluating the function main (by > main in the interpreter mode) runs all of the tests in the test suite.

Alternatively, you can compile the code and execute it in the terminal mode:

> ghc hw1-skeleton.hs and then

> ./hw1-skeleton which has the same effect as when you do > main in the interpreter mode.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSCE314 Homework 1-Programming in Haskell
$25