CS 550
Spring 2015
Programming Languages
Thursdays 18:30-21:20
Korman Center 104C

Geoffrey Mainland
University Crossings 106
Teaching Assistant:
Mark Boady
Warning! This material is for an old version of the course.

For this homework, you will implement and test interval sets.

We have provided you with a shell for your solution here. Please extract this tarball in your ~/cs550/git directory and immediately commit the resulting hw6 directory. You can do this as follows:

$ cd ~/cs550/git
$ wget 'https://www.cs.drexel.edu/~mainland/courses/cs550-201435/homework/hw6.tar.gz'
$ tar xf hw6.tar.gz
$ git add hw6
$ git commit -m "Initial check-in for homework 6."

You may build a binary unit-tests that will execute your unit tests by typing make unit-tests. Run the resulting binary, named unit-tests, to run your unit tests.

Your code must run on tux under the version of ghc that we provide.

Be sure to commit your work to the repository.

This assignment is worth 100 points. There are 101 possible points.

About Interval Sets

Interval sets are a way of representing sets of integers. Sets are represented as a collection of intervals, and intervals are represented by a pair of integers. The type of interval sets that we will use in Haskell is:

type IntSet = [(Int,Int)]

For example, to represent the set $\{2,3,4,10,11,12,13\}$ we use [(2,4),(10,13)]. The interval (2,4) means that we have $2,3,4$ in the set; the interval (10,13) means that we have $10,11,12,13$ in the set.

The intervals used in interval sets should satisfy the following invariants:

  1. For any interval (x,y) in a set, x should not be greater than y (so the intervals are not empty); for example (2,5) and (4,4) are OK, but not (7,2).

  2. Two intervals in the same set should not be overlapping (or even touching); for example we cannot have the intervals (2,6) and (4,10) in the same set; they should be replaced by (2,10). We cannot have (3,6) and (7,11) in the same set either; they should be replaced by (3,11)

  3. The intervals in a given interval set should occur in ascending order; for example [(1,3),(5,9),(15,16)] is fine, but [(5,10),(1,3)] is not.

Problem 1: Implementation (50 points total)

Implement the following functions on interval sets, keeping the above properties in mind:

emptyS  :: IntSet                        -- creating the empty set
memberS :: Int -> IntSet -> Bool         -- checking if an integer is in the set
insertS :: Int -> IntSet -> IntSet       -- adding an element
mergeS  :: IntSet -> IntSet -> IntSet    -- set union
deleteS :: Int -> IntSet -> IntSet       -- delete an element from the set

The functions should be robust; for example inserting an element that already exists (or deleting an element that does not exist) should simply return the original set.

Please put your implementation in the file IntSet.hs which we have provided. You must not change the definition of the IntSet type or the type signatures of the above functions.

You will likely want to solve problem 1 and problem 2 simultaneously—your tests will help you write a correct implementation.

Each function is worth 10 points.

I suggest you write and test emptyS and memberS first. Then move on to writing deleteS and its test next. Finally, tackle insertS, mergeS, and their tests.

Hint: Write insertS in terms of mergeS. How do you represent an IntSet containing a single value?

Problem 2: Testing (50 points total)

Write unit tests for all five of your functions above. Put your unit tests in the file UnitTests.hs. Test coverage is worth 10 points per function; you will not receive credit for the one unit test we provide. You will likely want to use the function fromList in UnitTests.hs.

Problem 3: Homework Statistics (1 point)

How long did it take you to complete each problem? Please tell us in a comment in IntSet.hs. You must tell us how long each problem took you, expressed as a quantitative measure of time, to receive the point.