Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
fp2024:hw1 [2024/03/21 17:49] pdmatei |
fp2024:hw1 [2024/03/21 19:12] (current) pdmatei |
||
---|---|---|---|
Line 69: | Line 69: | ||
</code> | </code> | ||
- | **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'' | + | **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 initial value ''b'' should produce: ''( (b op x) op y) op z'' |
<code scala> | <code scala> | ||
def foldLeftSet | def foldLeftSet | ||
Line 78: | Line 78: | ||
</code> | </code> | ||
- | **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. | + | **9.** Implement an alternative to the previous function, namely **foldRight**. Applying ''foldRight'' on the set ''{x,y,z}'' with initial value ''b'' should produce: ''a op (b op (c op b))''. Use direct recursion instead of tail recursion. |
<code scala> | <code scala> | ||
def foldRightSet | def foldRightSet | ||
Line 87: | Line 87: | ||
</code> | </code> | ||
- | **10.** Implement operation ''filter'' which takes a set and returns another one containing only those elements that satisfy the predicate: | + | **10.** Implement operation ''filter'' which takes a set and returns the set containing only those elements $math[e] for which $math[p(e)] is true: |
<code scala> | <code scala> | ||
- | def filter(p: Int => Boolean)(set: Set): Set = ??? | + | def filter(p: Int => Boolean)(s: Set): Set = ??? |
</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. | + | **11.** Implement a function which **partitions** a set into two sets. The left-most contains those elements that satisfy the predicate $math[p], 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 a 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> | <code scala> | ||
- | def partition(p: Int => Boolean)(set: Set): (Set,Set) = ??? | + | def partition(p: Int => Boolean)(s: Set): (Set,Set) = ??? |
</code> | </code> | ||
- | **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). | + | **12.** Implement a function ''forall'' which checks if all elements in a given range from a set satisfy the predicate $math[p]. (For instance $math[p] might be used to describe even numbers). |
<code scala> | <code scala> | ||
def forall(cond: Int => Boolean) // condition to be checked | def forall(cond: Int => Boolean) // condition to be checked | ||
(start: Int, stop: Int) // start,stop values (inclusive) | (start: Int, stop: Int) // start,stop values (inclusive) | ||
- | (set: Set): Boolean // set to be checked | + | (s: Set): Boolean // set to be checked |
= ??? | = ??? | ||
</code> | </code> | ||
Line 107: | Line 107: | ||
**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)]. | **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)]. | ||
- | **14.** Implement the function ''setOfDivByK'' which returns the set of integers divisible by a value ''k''. Use the appropriate functions you have defined. | + | **14.** Implement the function ''setOfDivByK'' which returns the set of integers divisible by a value ''k''. Use the appropriate functions you have defined previously in the homework. |
<code scala> | <code scala> | ||
def setOfDivByK(k: Int): Set = ?? | def setOfDivByK(k: Int): Set = ?? | ||
Line 120: | Line 120: | ||
===== Submission rules ===== | ===== Submission rules ===== | ||
- | * Please follow the [[fp2023:submission-guidelines| Submission guidelines]] which are the same for all homework. | + | * Please follow the [[fp2024: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. | + | * To solve your homework, download the {{:fp2024: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. |