Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
fp2023:hw1 [2023/03/21 19:50] pdmatei created |
fp2023:hw1 [2023/03/28 14:11] (current) pdmatei |
||
---|---|---|---|
Line 9: | Line 9: | ||
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: | 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> | <code scala> | ||
- | Int => Boolean | + | type Set = Int => Boolean |
</code> | </code> | ||
- | For instance, the set $math[\{1,2,3\}] may be encoded by the anonymous function: | + | For instance, the set $math[\{1,2,3\}] will be encoded by the anonymous function: |
<code scala> | <code scala> | ||
(x: Int) => (x == 1 || x == 2 || x == 3) | (x: Int) => (x == 1 || x == 2 || x == 3) | ||
Line 29: | Line 29: | ||
**1.** Write a function ''singleton'' which takes an integer and returns **the set** containing only that integer: | **1.** Write a function ''singleton'' which takes an integer and returns **the set** containing only that integer: | ||
<code scala> | <code scala> | ||
- | def singleton(x: Int): Int => Boolean = ??? | + | def singleton(x: Int): Set = ??? |
</code> | </code> | ||
Line 37: | Line 37: | ||
**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: | **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> | <code scala> | ||
- | def member(set: Int => Boolean)(e: Int): Boolean = ??? | + | def member(e: Int)(set: Set): Boolean = ??? |
</code> | </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(. | + | **3.** Write a function ''ins'' which inserts a new element in a set. More precisely, given $math[x] and $math[set], ''ins'' returns a new set $math[\{x\} \cup set]. |
<code scala> | <code scala> | ||
- | def fromBounds(start: Int, stop: Int): Int => Boolean = ??? | + | def ins(x: Int)(set: Set): Set = ??? |
</code> | </code> | ||
- | **4.** Write a function which performs the intersection of two sets: | + | **4.** 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 $math[start \leq stop] (you do not need to check this condition in your implementation). |
<code scala> | <code scala> | ||
- | def intersection(set1: Int => Boolean, set2: Int => Boolean): Int => Boolean = ??? | + | def fromBounds(start: Int, stop: Int): Set = ??? |
</code> | </code> | ||
**5.** Write the function which performs the union of two sets: | **5.** Write the function which performs the union of two sets: | ||
<code scala> | <code scala> | ||
- | def union(set1: Int => Boolean, set2: Int => Boolean): Int => Boolean = ??? | + | def union(set1: Set, set2: Set): Set = ??? |
</code> | </code> | ||
- | **6.** Write a function which computes the sum of all elements from a set, for given **bounds**. Use a tail-end recursive function: | + | **6.** Write a function which computes the complement of a set with respect to the set of integers: |
<code scala> | <code scala> | ||
- | def sumSet(start: Int, stop: Int, set: Int => Boolean): Int = { | + | def complement(s1: Set): Set = ??? |
+ | </code> | ||
+ | |||
+ | **7.** Write a function which computes the sum of value ''b'' to all elements from a set, for given **bounds**. Use a tail-end recursive function: | ||
+ | <code scala> | ||
+ | def sumSet(b: Int)(start: Int, stop: Int)(set: Set): Int = { | ||
def auxSum(crt: Int, acc: Int): Int = ??? | def auxSum(crt: Int, acc: Int): Int = ??? | ||
??? | ??? | ||
Line 64: | Line 69: | ||
</code> | </code> | ||
- | **7.** Generalise the previous function such that we can **fold** a set using any binary commutative operation over integers: | + | **8.** Generalise the previous function such that we can **fold** a set using any binary commutative operation over integers. Make sure this is a **left** fold: Folding the set: ''{x,y,z}'' with ''b'' should produce: ''( (b op x) op y) op z'' |
<code scala> | <code scala> | ||
- | def foldSet( | + | def foldLeftSet |
- | start: Int, // bounds (inclusive) | + | (b:Int) // initial value |
- | stop: Int, | + | (op: (Int,Int) => Int) // folding operation |
- | op: (Int, Int) => Int, // folding operation | + | (start: Int, stop: Int) // bounds (inclusive) |
- | initial: Int, // initial value | + | (set: Set): Int = ??? // the set to be folded |
- | set: Int => Boolean // the set to be folded | + | |
- | ): Int = ??? | + | |
</code> | </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). | + | **9.** Implement an alternative to the previous function, namely **foldRight**. Applying ''foldRight'' on the set ''{x,y,z}'' with ''b'' should produce: ''a op (b op (c op b))''. Use direct recursion instead of tail recursion. |
<code scala> | <code scala> | ||
- | def forall( | + | def foldRightSet |
- | start: Int, // start value (inclusive) | + | (b:Int) // initial value |
- | stop: Int, // stop value (inclusive) | + | (op: (Int,Int) => Int) // folding operation |
- | condition: Int => Boolean, // condition to be checked | + | (start: Int, stop: Int) // bounds (inclusive) |
- | set: Int => Boolean // set to be checked | + | (set: Set): Int = ??? // the set to be folded |
- | ): Boolean = ??? | + | |
</code> | </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)]. | + | **10.** Implement operation ''filter'' which takes a set and returns another one containing only those elements that satisfy the predicate: |
<code scala> | <code scala> | ||
- | /* implement a function exists, using forall */ | + | def filter(p: Int => Boolean)(set: Set): Set = ??? |
- | 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> | </code> | ||
+ | **11.** Implement a function which **partitions** a set into two sets. The left-most contains those elements that satisfy the predicate, while the right-most contains those elements that do not satisfy the predicate. Use pairs. A pair is constructed with simple parentheses. E.g. ''(1,2)'' is a pair of two integers. Suppose ''val p: (Int,Int)'' is another pair of two integers. Then ''p._1'' is the left-most part of the pair while ''p._2'' is the right-most part of the pair. | ||
+ | <code scala> | ||
+ | def partition(p: Int => Boolean)(set: Set): (Set,Set) = ??? | ||
+ | </code> | ||
- | ===== Submission rules ===== | + | **12.** 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(cond: Int => Boolean) // condition to be checked | ||
+ | (start: Int, stop: Int) // start,stop values (inclusive) | ||
+ | (set: Set): Boolean // set to be checked | ||
+ | = ??? | ||
+ | </code> | ||
- | ==== Project format ==== | + | **13.** 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)]. |
- | * **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**. | + | **14.** Implement the function ''setOfDivByK'' which returns the set of integers divisible by a value ''k''. Use the appropriate functions you have defined. |
- | * To solve your homework, download the {{:fp:h1-fsets.zip| 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**. | + | <code scala> |
- | * 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). | + | def setOfDivByK(k: Int): Set = ?? |
- | * 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 ''???''. | + | </code> |
- | ==== Submission ==== | + | **15.** Implement the function ''moreDivs'' which verifies if ''set1'' contains more divisors of ''k'' than ''set2'', over the range ''[start,stop]''. Use any combination of the previous functions you have defined for your implementation. |
- | * Your submission should be an **archived file** with your solved project, named via the convention specified previously. | + | <code scala> |
- | * **All homework must be submitted via moodle. Submissions sent via email will not be graded!**. | + | def moreDivs(k: Int)(start: Int, stop:Int)(set1: Set, set2: Set): Boolean = ??? |
- | * **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). | + | </code> |
- | ==== 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 ==== | + | ===== Submission rules ===== |
- | * 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. | + | * Please follow the [[fp2023:submission-guidelines| Submission guidelines]] which are the same for all homework. |
+ | * To solve your homework, download the {{:fp2023:hw1-functions-as-sets.zip|Project template}}, import it in IntellIJ, and you are all set. Do not rename the project manually, as this may cause problems with IntellIJ. | ||