Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
fp:lab03 [2022/02/24 13:02] pdmatei |
fp:lab03 [2022/03/18 15:31] (current) pdmatei |
||
---|---|---|---|
Line 5: | Line 5: | ||
* implement **curry** and **uncurry** functions, and how they should be properly used (review lecture). | * implement **curry** and **uncurry** functions, and how they should be properly used (review lecture). | ||
- | **3.1.** Define the function ''foldRange'' 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, 3 will be 6. ''foldRange'' should be curried (it will take the operation and return another function which expects the bounds). | + | **3.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 20: | Line 20: | ||
</code> | </code> | ||
- | **3.3. (!) ** Let $math[count_k(n) = k + 2k + 3k + ... x*k], with $math[ x*k \leq n] be the sum of all multiples of $math[k] within the range 1,n. Write a function ''alldivs'' which computes the sum: $math[count_1(n) + count_2(n) + ... + count_k(n)]. (Hint, use ''foldConditional''). | + | **3.3. [//should be revised//] ** Let $math[count_k(n) = k + 2k + 3k + ... x*k], with $math[ x*k \leq n] be the sum of all multiples of $math[k] within the range 1,n. Write a function ''alldivs'' which computes the sum: $math[count_1(n) + count_2(n) + ... + count_k(n)]. (Hint, use ''foldConditional''). |
<code scala> | <code scala> | ||
Line 26: | Line 26: | ||
</code> | </code> | ||
- | **3.4.** Write a function ''foldMap'' which takes values $math[a_1, a_2, \ldots, a_k] from a range and computes $math[f(a_1) op f(a_2) op \ldots f(a_k)]. | + | **3.4.** Write a function ''foldMap'' which takes values $math[a_1, a_2, \ldots, a_k] from a range and computes $math[f(a_1)\;op\;f(a_2)\;op\;\ldots f(a_k)]. |
+ | <code scala> | ||
+ | def foldMap(op: (Int,Int) => Int, f: Int => Int)(start: Int, stop: Int): Int = ??? | ||
+ | </code> | ||
+ | |||
+ | **3.5.** Write a function which computes $math[1 + 2^2 + 3^2 + \ldots + (n-1)^2 + n^2] using ''foldMap''. | ||
+ | <code scala> | ||
+ | def sumSquares(n: Int): Int = ??? | ||
+ | </code> | ||
+ | |||
+ | **3.6.** Write a function ''hasDivisor'' which checks if a range contains a multiple of k. Use ''foldMap'' and choose ''f'' carefully. | ||
+ | <code scala> | ||
+ | def hasDivisor(k: Int, start: Int, stop: Int): Boolean = ??? | ||
+ | </code> | ||
+ | |||
+ | **3.7.** We can compute the sum of an area defined by a function within a range a,b (the integral of that function given the range), using the following recursive scheme: | ||
+ | * if the range is small enough, we treat f as a line (and the area as a trapeze). It's area is $math[(f(a) + f(b))(b-a)/2]. | ||
+ | * otherwise, we compute the mid of the range, we recursively compute the integral from a to mid and from mid to b, and add-up the result. | ||
+ | |||
+ | Implement the function ''integrate'' which computes the integral of a function f given a range: | ||
+ | <code scala> | ||
+ | def integrate(f: Double => Double)(start: Double, stop: Double): Double = ??? | ||
+ | </code> |