Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
fp2023:lab02 [2023/03/10 10:41]
pdmatei
fp2023:lab02 [2024/03/15 10:48] (current)
pdmatei
Line 18: Line 18:
 } }
 </​code>​ </​code>​
 +
  
 **2.2.** Implement a tail-recursive function that computes the greatest common divisor of a natural number: **2.2.** Implement a tail-recursive function that computes the greatest common divisor of a natural number:
Line 24: Line 25:
 def gcd(a: Int, b: Int): Int = ??? def gcd(a: Int, b: Int): Int = ???
 </​code>​ </​code>​
 +
  
 **2.3.** Write a tail-recursive function takes an integer $math[n] and computes the value $math[1 + 2^2 + 3^2 + ... + (n-1)^2 + n^2]. (Hint: use inner functions). **2.3.** Write a tail-recursive function takes an integer $math[n] and computes the value $math[1 + 2^2 + 3^2 + ... + (n-1)^2 + n^2]. (Hint: use inner functions).
Line 37: Line 39:
 </​code>​ </​code>​
  
-**2.5.** (!) 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.+**2.5.** (!) 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.
  
 <code scala> <code scala>
Line 43: Line 45:
 </​code>​ </​code>​
  
-**2.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_0 - x_1 - \ldots - x_n - x]. Use the most appropriate type of recursion for this task.+**2.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_0 - (x_1 - (x_2 - (\ldots - (x_n - x)\ldots )]. Use the most appropriate ​**type of recursion** for this task.