Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
pp:scalalab2 [2022/05/16 00:12] vbadoiu [L10. List and Datatypes and Functional data representation] |
pp:scalalab2 [2022/05/19 00:16] (current) mihai.calitescu resolved typos |
||
---|---|---|---|
Line 11: | Line 11: | ||
**1.1.1.** Write a function which returns true if a list of integers has at least k elements. Use patterns. | **1.1.1.** Write a function which returns true if a list of integers has at least k elements. Use patterns. | ||
<code scala> | <code scala> | ||
- | def atLeastk(k: Int, l: List[Int]): Boolean = | + | def atLeastk(k: Int, l: List[Int]): Boolean = { |
if (k == 0) ??? | if (k == 0) ??? | ||
else ??? | else ??? | ||
- | } | + | } |
</code> | </code> | ||
Line 24: | Line 24: | ||
- | **1.1.4.** Write a function which takes a predicate ''p: Int => Boolean'', a list ''l'' and returns a sublist of ''l'' containing those elements for which ''p'' is true. The function should be **curried**. | + | **1.1.3.** Write a function which takes a predicate ''p: Int => Boolean'', a list ''l'' and returns a sublist of ''l'' containing those elements for which ''p'' is true. The function should be **curried**. |
<code scala> | <code scala> | ||
def takeP(p: Int => Boolean)(l: List[Int]): List[Int] = ??? | def takeP(p: Int => Boolean)(l: List[Int]): List[Int] = ??? | ||
Line 30: | Line 30: | ||
</code> | </code> | ||
- | **1.1.5.** Write a function which uses a predicate to partition (split) a list. | + | **1.1.4.** Write a function which uses a predicate to partition (split) a list. |
<code scala> | <code scala> | ||
def part(p: Int => Boolean)(l: List[Int]): (List[Int], List[Int]) = ??? | def part(p: Int => Boolean)(l: List[Int]): (List[Int], List[Int]) = ??? |