Differences

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

Link to this comparison view

Next revision
Previous revision
fp2025:lab05 [2025/03/26 15:55]
pdmatei created
fp2025:lab05 [2026/04/16 10:53] (current)
silviu
Line 1: Line 1:
-====== Lab 4. Functional vs Object-Oriented decomposition ======+====== Lab 5. Functional vs Object-Oriented decomposition ======
  
-==== 4.1. The type Nat ===== +==== 5.1. The type Nat ===== 
 Consider the following type defined to represent natural numbers: Consider the following type defined to represent natural numbers:
 <code scala> <code scala>
Line 17: Line 17:
 When implementing the following methods, think about whether or not they are **local** (are they best implemented using functional or OO decomposition?​) When implementing the following methods, think about whether or not they are **local** (are they best implemented using functional or OO decomposition?​)
  
-**4.1.1.** Implement the method ''​isZero''​ which checks if a number is equal to ''​Zero''​ or not.+**5.1.1.** Implement the method ''​isZero''​ which checks if a number is equal to ''​Zero''​ or not.
  
-**4.1.2.** Implement the addition method over natural numbers.+**5.1.2.** Implement the addition method over natural numbers.
  
-**4.1.3.** Implement the subtraction method over natural numbers. If $math[n > m] then $math[m - n = 0].+**5.1.3.** Implement the subtraction method over natural numbers. If $math[n > m] then $math[m - n = 0].
  
-**4.1.4.** Implement ''​greater''​ which checks if a natural number is strictly larger than the other.+**5.1.4.** Implement ''​greater''​ which checks if a natural number is strictly larger than the other.
  
-**4.1.5.** Implement a function which converts a Nat to a Scala Int.+**5.1.5.** Implement a function which converts a Nat to a Scala Int.
  
-==== 4.2. The type OList (Object-Oriented implementation of lists) ====+==== 5.2. The type OList (Object-Oriented implementation of lists) ====
  
 Start with the following trait, which encodes lists over integers. Start with the following trait, which encodes lists over integers.
Line 45: Line 45:
 </​code>​ </​code>​
  
-**4.2.1.** Implement the constructors for the **empty list** as well as for the **nonempty list of integers**.+**5.2.1.** Implement the constructors for the **empty list** as well as for the **nonempty list of integers**.
  
-**4.2.2.** Implement ''​head''​ and ''​tail''​ accordingly.+**5.2.2.** Implement ''​head''​ and ''​tail''​ accordingly.
  
-**4.2.3.** Implement ''​foldRight''​. Follow the same strategy as in the previous labs. Also implement foldLeft. Make sure it is tail-recursive.+**5.2.3.** Implement ''​foldRight''​. Follow the same strategy as in the previous labs. Also implement foldLeft. Make sure it is tail-recursive.
  
  
-**4.2.5.** Implement ''​filter''​. Try and use ''​foldRight''​ in your implementation. The following diagram illustrates the behaviour of ''​filter'':​+**5.2.5.** Implement ''​filter''​. Try and use ''​foldRight''​ in your implementation. The following diagram illustrates the behaviour of ''​filter'':​
 <​code>​ <​code>​
    1 2 3 4 5 6 7 8 9 10 11    1 2 3 4 5 6 7 8 9 10 11
Line 59: Line 59:
 </​code>​ </​code>​
  
-**4.2.4.** Implement ''​indexOf''​ which retrieves the position of an element in a list if it exists and -1 otherwise. Try and use an appropriate type of fold for your implementation.+**5.2.4.** Implement ''​indexOf''​ which retrieves the position of an element in a list if it exists and -1 otherwise. Try and use an appropriate type of fold for your implementation.
  
  
-**4.2.6.** Implement ''​map''​. Try and use an appropriate type of fold in your implementation. The following diagram illustrates the behaviour of map:+**5.2.6.** Implement ''​map''​. Try and use an appropriate type of fold in your implementation. The following diagram illustrates the behaviour of map:
 <​code>​ <​code>​
    1 2 3 4 5  6  7  8  9  10    1 2 3 4 5  6  7  8  9  10
Line 69: Line 69:
 </​code>​ </​code>​
  
-**4.2.7.** Implement ''​partition''​. It is supposed to return a pair of lists. (!!) Try and use an appropriate type of fold in your implementation. The following diagram illustrates the behaviour of partition:+**5.2.7.** Implement ''​partition''​. It is supposed to return a pair of lists. (!!) Try and use an appropriate type of fold in your implementation. The following diagram illustrates the behaviour of partition:
 <​code>​ <​code>​
    1 2 3 4 5 6 7 8 9 10    1 2 3 4 5 6 7 8 9 10
Line 76: Line 76:
 </​code>​ </​code>​
  
-**4.2.8.** Implement ''​slice''​. The following diagram illustrates the behaviour of slice:+**5.2.8.** Implement ''​slice''​. The following diagram illustrates the behaviour of slice:
 <​code>​ <​code>​
    1 2 3 4 5 6 7 8 9 10    1 2 3 4 5 6 7 8 9 10
Line 83: Line 83:
 </​code>​ </​code>​
  
-**4.2.8.** Implement ''​forall''​. Try and use an appropriate type of fold in your implementation. The following diagram illustrates the behaviour of forall:+**5.2.8.** Implement ''​forall''​. Try and use an appropriate type of fold in your implementation. The following diagram illustrates the behaviour of forall:
 <​code>​ <​code>​
    1 2 3 4 5 6 7 8 9 10    1 2 3 4 5 6 7 8 9 10