Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
pp:2024:scala:l02 [2024/03/14 14:49] robert.stoica2205 |
pp:2024:scala:l02 [2025/03/05 10:47] (current) pdmatei |
||
|---|---|---|---|
| Line 31: | Line 31: | ||
| **2.1.4** Write a function ''realtrycatch'' where t and c take no parameters and produce a result upon evaluation. If an error occurs (try function returns 0), the catch function will be called instead. | **2.1.4** Write a function ''realtrycatch'' where t and c take no parameters and produce a result upon evaluation. If an error occurs (try function returns 0), the catch function will be called instead. | ||
| <code scala> | <code scala> | ||
| - | def realtrycatch(t : => Int, c: => Int): Int = { | + | def realtrycatch(t : => Int, c: => Int): Int = { |
| ??? | ??? | ||
| } | } | ||
| Line 37: | Line 37: | ||
| ===== 2.2 Custom high order functions ===== | ===== 2.2 Custom high order functions ===== | ||
| + | |||
| **2.2.1** Define the function ''foldWith'' which uses an operation ''op'' to reduce a range of integers to a value. For instance, given that ''op'' is addition (+), the result of folding the range 1 to 3 will be 1+2+3=6. ''foldWith'' should be curried (it will take the operation and return another function which expects the bounds). | **2.2.1** Define the function ''foldWith'' which uses an operation ''op'' to reduce a range of integers to a value. For instance, given that ''op'' is addition (+), the result of folding the range 1 to 3 will be 1+2+3=6. ''foldWith'' should be curried (it will take the operation and return another function which expects the bounds). | ||
| <code scala> | <code scala> | ||
| Line 67: | Line 68: | ||
| def compare(x: Int, y: Int, z: Int): Int = | def compare(x: Int, y: Int, z: Int): Int = | ||
| { | { | ||
| - | if x > y && x > z then | + | if (x > y && x > z) x |
| - | x | + | else if (y > x && y > z) y |
| - | else if y > x && y > z then | + | else z |
| - | y | + | |
| - | else | + | |
| - | z | + | |
| } | } | ||
| </code> | </code> | ||