Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| fp2023:lab05 [2023/03/28 17:33] pdmatei | fp2023:lab05 [2023/03/31 12:58] (current) pdmatei | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| def add(other: Nat): Nat | def add(other: Nat): Nat | ||
| def subtract(other: Nat): Nat | def subtract(other: Nat): Nat | ||
| - | def greater(other: Nat): Nat | + | def greater(other: Nat): Boolean | 
| def toInt: Int | def toInt: Int | ||
| } | } | ||
| Line 34: | Line 34: | ||
| def head: Int | def head: Int | ||
| def tail: OList | def tail: OList | ||
| - | def foldRight(acc: Int)(op: (Int,Int) => Int): Int | + | def foldRight[B](acc: B)(op: (Int,B) => B): B | 
| + | def foldLeft[B](acc: B)(op: (B,Int) => B): B | ||
| def indexOf(i: Int): Int | def indexOf(i: Int): Int | ||
| def filter(p: Int => Boolean): OList | def filter(p: Int => Boolean): OList | ||
| Line 48: | Line 49: | ||
| **4.2.2.** Implement ''head'' and ''tail'' accordingly. | **4.2.2.** Implement ''head'' and ''tail'' accordingly. | ||
| - | **4.2.3.** Implement ''foldRight''. Follow the same strategy as in the previous labs. | + | **4.2.3.** Implement ''foldRight''. Follow the same strategy as in the previous labs. Also implement foldLeft. Make sure it is tail-recursive. | 
| - | **4.2.4.** Implement ''indexOf'' which retrieves the position of an element in a list if it exists and -1 otherwise. Try and use ''foldRight'' for your implementation. | ||
| - | **4.2.5.** Implement ''filter''. Try and use ''foldRight'' in your implementation. The following diagram illustrates the behaviour of filter: | + | **4.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.6.** Implement ''map''. Try and use ''foldRight'' in your implementation. The following diagram illustrates the behaviour of map: | + | **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. | 
| + | |||
| + | |||
| + | **4.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 66: | Line 69: | ||
| </code> | </code> | ||
| - | **4.2.7.** Implement ''partition''. It is supposed to return a pair of lists. (!!) Try and use ''foldRight'' in your implementation. The following diagram illustrates the behaviour of partition: | + | **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: | 
| <code> | <code> | ||
| 1 2 3 4 5 6 7 8 9 10 | 1 2 3 4 5 6 7 8 9 10 | ||
| Line 80: | Line 83: | ||
| </code> | </code> | ||
| - | **4.2.8.** Implement ''forall''. Try and use ''foldRight'' in your implementation. The following diagram illustrates the behaviour of forall: | + | **4.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 | ||