Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
pp:2023:scala:l01 [2023/03/06 15:38] tpruteanu |
pp:2023:scala:l01 [2023/03/15 18:51] (current) pdmatei |
||
---|---|---|---|
Line 35: | Line 35: | ||
**1.1.4.** Write a function which computes the sum of all natural numbers within a range. Use **two styles** to write this function: direct recursion, and tail recursion. | **1.1.4.** Write a function which computes the sum of all natural numbers within a range. Use **two styles** to write this function: direct recursion, and tail recursion. | ||
<code scala> | <code scala> | ||
- | def sumNats(start: Int, stop: Int) = ??? | + | def sumNats(start: Int, stop: Int): Int = ??? |
- | def tailSumNats(start: Int, stop: Int) = ??? | + | def tailSumNats(start: Int, stop: Int): Int = ??? |
</code> | </code> | ||
Line 44: | Line 44: | ||
</code> | </code> | ||
- | **1.1.6.** (!) Write a function which takes an initial value $math[x] and a range of values $math[x_0, x_1, \ldots, x_n] and computes $math[x - x_0 - x_1 - \ldots x_n]. Use the most appropriate type of recursion for this task. | + | **1.1.6.** (!) Write a function which takes an initial value $math[x] and a range of values $math[x_0, x_1, \ldots, x_n] and computes $math[(\ldots((x - x_0) - x_1) - \ldots x_n)]. Use the most appropriate type of recursion for this task. |
<code scala> | <code scala> | ||
- | def subtractRange(x: Int, start: Int, stop: Int) = ??? | + | def subtractRange(x: Int, start: Int, stop: Int): Int = ??? |
</code> | </code> | ||
- | **1.1.7.** (!) Write a function which takes an initial value $math[x] and a range of values $math[x_0, x_1, \ldots, x_n] and computes $math[x_0 - x_1 - \ldots - x_n - x]. Use the most appropriate type of recursion for this task. | + | **1.1.7.** (!) Write a function which takes an initial value $math[x] and a range of values $math[x_0, x_1, \ldots, x_n] and computes $math[x_0 - (x_1 - \ldots - (x_n - x)\ldots)]. Use the most appropriate type of recursion for this task. |