CS 355
Functional Programming: Sets

Due: 11/30/03


In this assignment we will be using lists to implement "sets" - collections of items in which no duplicates are allowed.

Part A: SML

  1. Write a function isMember(x,S) that returns true if x is somewhere in the "set" S, false if it is not.
  2. Write a function setDiff(A, B) that returns the "set difference" A-B, i.e., a list of elements that are members of A but not of B.
  3. Write a function intersection(A,B) that returns a list of those elements that are in both A and B.
  4. Write a function union(A,B) that returns a list of those elements that are in A and/or are in B.
  5. Write a function setsEQ(A,B) that returns true if the sets A and B contain the same elements (even if the elements are written in different orders), false if they do not.

Your functions in 1-5 may assume that each input "set" contains no duplicate members. Your outputs in questions 2-4 should also never contain duplicates.

Part B: Scheme

Repeat problems 1-5 in Scheme. (Of course, in Scheme the function calls would be written as s-expressions. E.g., (union A B) instead of ML's union(A,B).)

Details

The answer to each question should be placed in a separate file. The ML files are to be named q1.sml, q2.sml, q3.sml, q4.sml, and q5.sml and the Scheme files as q1.scm, q2.scm, q3.scm, q4.scm, and q5.scm.

In each case, loading the q?.* file (via the ML "use" or the Scheme "load" command) into sml/scheme should result in the declaration of the function requested in that problem. If your implementation of the reuqested functions call upon other functions that you have written, those functions should also be loaded. For example, if your implementation of setDiff uses your isMember function, then q2.sml must either include the actual code of isMember, or must include the line

  use "q1.sml";

Notes:

To run SML, type
    ~zeil/bin/sml

To load a file into your SML session, use the "use" function:

    use "filename";

To end your SML session, use control-D (the "standard" Unix signal for end-of-file).

To run scheme

    ~zeil/bin/scheme

To load a file into your Scheme session, use the "load" function:

    (load "filename")

To end your scheme session, use control-D (the "standard" Unix signal for end-of-file).

Additional info on both SML and Scheme is available on the "Library" page of the course web site.

Submitting

When you are ready to submit your assignment, go here.

You will receive an automatic e-mailed grade report.

There are a large number of tests. They break down as follows: