Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:2025:scala:l04 [2025/03/19 20:41] cata_chiru |
pp:2025:scala:l04 [2025/03/19 20:51] (current) cata_chiru |
||
---|---|---|---|
Line 17: | Line 17: | ||
</code> | </code> | ||
- | **4.1.2.** Write a function which returns the first ''n'' elements from a given list. The function should **not** be implemented as tail-recursive. | + | **4.1.2.** 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> | + | |
- | def take(n: Int, l: List[Int]): List[Int] = ??? | + | |
- | //take(3,List(1,2,3,4,5)) = List(1,2,3) | + | |
- | </code> | + | |
- | + | ||
- | **4.1.3.** Write a function which //drops// the first ''n'' elements from a given list. | + | |
- | + | ||
- | <code scala> | + | |
- | def drop(n: Int, l: List[Int]): List[Int] = ??? | + | |
- | //drop(3,List(1,2,3,4,5)) = List(4,5) | + | |
- | </code> | + | |
- | + | ||
- | **4.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**. | + | |
<code scala> | <code scala> | ||
Line 38: | Line 24: | ||
</code> | </code> | ||
- | **4.1.5.** Write a function which uses a predicate to partition (split) a list. | + | **4.1.3.** 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]) = ??? | ||
// part(_%2 == 0)(List(1,2,3,4,5,6)) = (List(2,4,6),List(1,3,5)) | // part(_%2 == 0)(List(1,2,3,4,5,6)) = (List(2,4,6),List(1,3,5)) | ||
- | </code> | ||
- | |||
- | **4.1.6.** Write a function that reverses the elements of a list. Use a ''fold'' function (determine if it should be right or left). | ||
- | <code scala> | ||
- | def rev(l: List[Int]): List[Int] = ??? | ||
- | // rev(List(1,2,3,4,5,6)) = List(6,5,4,3,2,1) | ||
</code> | </code> | ||
Line 89: | Line 69: | ||
</code> | </code> | ||
- | **4.2.5.** Implement a function that return the domains without the dot (ex. ''gmail''). | + | **4.2.5.** Implement a function that returns the domains without the dot (ex. ''gmail''). |
<code scala> | <code scala> | ||
def domains(list: List[Str]): List[Str] = ??? | def domains(list: List[Str]): List[Str] = ??? | ||
</code> | </code> | ||
- | |||
- | |||
- | |||
==== 4.3. Gradebooks ==== | ==== 4.3. Gradebooks ==== |