Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== H01. Functional Sets ====== ===== Problem statement ===== 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 $math[A \subseteq U] is a function $math[f: U \rightarrow \{0,1\}] which assigns $math[f(x) = 1] for each element $math[x \in A] and $math[f(x) = 0] for each element $math[x \not\in A]. In our implementation, $math[U] will be the set of integers, hence we shall encode only **sets of integers**. Hence, the type of a set will be: <code scala> Int => Boolean </code> For instance, the set $math[\{1,2,3\}] may be encoded by the anonymous function: <code scala> (x: Int) => (x == 1 || x == 2 || x == 3) </code> Also, the empty set can be encoded as: <code scala> (x: Int) => false </code> while the entire set of integers may be encoded as: <code scala> (x: Int) => true </code> **1.** Write a function ''singleton'' which takes an integer and returns **the set** containing only that integer: <code scala> def singleton(x: Int): Int => Boolean = ??? </code> 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: <code scala> def member(set: Int => Boolean)(e: Int): Boolean = ??? </code> **3.** Write a function ''fromBounds'' which takes two integer bounds ''start'' and ''stop'' and returns the set $math[\{start, start+1, \ldots, stop\}]. It is guaranteed that ''start <= stop'' (you do not need to check this condition in your implementation(. <code scala> def fromBounds(start: Int, stop: Int): Int => Boolean = ??? </code> **4.** Write a function which performs the intersection of two sets: <code scala> def intersection(set1: Int => Boolean, set2: Int => Boolean): Int => Boolean = ??? </code> **5.** Write the function which performs the union of two sets: <code scala> def union(set1: Int => Boolean, set2: Int => Boolean): Int => Boolean = ??? </code> **6.** Write a function which computes the sum of all elements from a set, for given **bounds**. Use a tail-end recursive function: <code scala> def sumSet(start: Int, stop: Int, set: Int => Boolean): Int = { def auxSum(crt: Int, acc: Int): Int = ??? ??? } </code> **7.** Generalise the previous function such that we can **fold** a set using any binary commutative operation over integers: <code scala> 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 = ??? </code> **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). <code scala> 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 = ??? </code> **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: $math[ \exists x. P(X) \iff \lnot \forall x.\lnot P(X)]. <code scala> /* 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 = ??? </code> ===== Submission rules ===== * **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:** is a submission has changes in other files, it **may not be graded**. * **All homework must be submitted via moodle. Submissions sent via email will not be graded!**. * To solve your homework, download the homework project and **rename it using the following convention: ''H1_<LastName>_<FirstName>''. (Example: ''H1_Popovici_Matei''). If your project does not adhere to this convention, it **may not be graded**.