Homework 1. Sets as functions

Sets are unordered collections of unique elements. There are several ways to store sets. One of them relies on characteristic functions. Such functional sets are especially useful if we expect many insert/retrieve operations and less traversals in our code.

A characteristic function of a set $ A \subseteq U$ is a function $ f: U \rightarrow \{0,1\}$ which assigns $ f(x) = 1$ for each element $ x \in A$ and $ f(x) = 0$ for each element $ x \not\in A$ .

In our implementation, $ U$ will be the set of integers, hence we shall encode only sets of integers. Hence, the type of a set will be:

Int => Boolean

For instance, the set $ \{1,2,3\}$ may be encoded by the anonymous function:

(x: Int) => (x == 1 || x == 2 || x == 3)

Also, the empty set can be encoded as:

(x: Int) => false

while the entire set of integers may be encoded as:

(x: Int) => true

1. Write a function singleton which takes an integer and returns the set containing only that integer:

def singleton(x: Int): Int => Boolean = ???

Note that singleton could have been equivalently defined as: def singleton(x: Int)(e: Int): Boolean = ???, however, the previous variant is more legible, in the sense that it highlights the idea that we are returning set objects, namely characteristic functions.

2. Write a function member which takes a set and an integer and checks if the integer is a member of the set. Note that member should be defined and called as a curry function:

def member(set: Int => Boolean)(e: Int): Boolean = ???

3. Write a function fromBounds which takes two integer bounds start and stop and returns the set $ \{start, start+1, \ldots, stop\}$ . It is guaranteed that start ⇐ stop (you do not need to check this condition in your implementation(.

def fromBounds(start: Int, stop: Int): Int => Boolean = ???

4. Write a function which performs the intersection of two sets:

def intersection(set1: Int => Boolean, set2: Int => Boolean): Int => Boolean = ???

5. Write the function which performs the union of two sets:

def union(set1: Int => Boolean, set2: Int => Boolean): Int => Boolean = ???

6. Write a function which computes the sum of all elements from a set, for given bounds. Use a tail-end recursive function:

  def sumSet(start: Int, stop: Int, set: Int => Boolean): Int = {
    def auxSum(crt: Int, acc: Int): Int = ???
    ???
  }

7. Generalise the previous function such that we can fold a set using any binary commutative operation over integers:

  def foldSet(
               start: Int,            // bounds (inclusive)
               stop: Int,
               op: (Int, Int) => Int, // folding operation
               initial: Int,          // initial value 
               set: Int => Boolean    // the set to be folded
             ): Int = ???

8. Implement a function forall which checks if all elements in a given range of a set satisfy a predicate (condition). (Such a condition may be that all elements from given bounds are even numbers).

  def forall(
              start: Int, // start value (inclusive)
              stop: Int, // stop value (inclusive)
              condition: Int => Boolean, // condition to be checked
              set: Int => Boolean // set to be checked
            ): Boolean = ???

9. Implement a function exists which checks if a predicate holds for some element from the range of a set. Hint: it is easier to implement exists using the logical relation: $ \exists x. P(X) \iff \lnot \forall x.\lnot P(X)$ .

  /* implement a function exists, using forall */
  def exists(
              start: Int, // start value (inclusive)
              stop: Int, // stop value (inclusive)
              condition: Int => Boolean, // condition to be checked
              set: Int => Boolean // set
            ): Boolean = ???

Project format

  • You should not change any other files of the project, except for the template-file. For this homework, the template-file is FSets.scala. Warning: if a submission has changes in other files, it may not be graded.
  • To solve your homework, download the Homework project and rename it using the following convention: HX_<LastName>_<FirstName>, where X is the homework number. (Example: H1_Popovici_Matei). If your project name disregards this convention, it may not be graded.
  • Each project file contains a profileID definition which you must fill out with your token ID received via email for this lecture. Make sure the token id is defined correctly. (Grades will be automatically assigned by token ID).
  • In order to be graded, the homework must compile. If a homework has compilation errors (does not compile), it will not be graded. Please take care to remove code that does not compile by replacing (or keeping) function bodies implemented with ???.

Submission

  • Your submission should be an archived file with your solved project, named via the convention specified previously.
  • All homework must be submitted via moodle. Submissions sent via email will not be graded!.
  • All homework must be submitted before the deadline. Submissions that miss the deadline (even by minutes) will not be graded. (All deadlines will be fixed at 8:00 AM, so that you can take advantage of an all-nighter, should you choose to).

Points

  • Points are assigned for each test (for a total of 100p), but the final grade will be assigned after manual review. Selectively, a homework may be required to be presented during lab for the final grade.

Integrity

  • Each homework uses public test-cases which you can use to guide and test your implementation. Most test-cases are simple, in order to be as easy to use as possible. If an implementation is written with the sole purpose of passing those specific tests, thus disregarding the statement, the entire homework will not be graded!
  • The homework must be solved individually - you are not allowed to share or to take code from other sources including the Internet.

We strongly encourage you to ask questions via the forum (instead of MS Teams) so that other students can benefit from the answers and discussion. You may ask questions about the homework during lab. You will receive feedback about your implementation ideas, but not on the actual written code.