Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:scalalab2 [2022/05/16 00:11] vbadoiu |
pp:scalalab2 [2022/05/19 00:16] (current) mihai.calitescu resolved typos |
||
---|---|---|---|
Line 4: | Line 4: | ||
* get familiar with **pattern matching** lists, as well as common list operations from Scala and how they work | * get familiar with **pattern matching** lists, as well as common list operations from Scala and how they work | ||
* get familiar with common **higher-order functions** over lists (partition, map, foldRight, foldLeft, filter) | * get familiar with common **higher-order functions** over lists (partition, map, foldRight, foldLeft, filter) | ||
+ | * learn about data types in Scala | ||
+ | * Use the knowledge in real world scenarios such as using sockets or iterating through the filesystem | ||
==== I. Common list operations ==== | ==== I. Common list operations ==== | ||
Line 9: | 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 22: | 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 28: | 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]) = ??? |