corrected / added midterm examples

This commit is contained in:
Louis Heredero 2025-04-15 13:12:25 +02:00
parent b53e0677cc
commit 9ad00e6182
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7

View File

@ -24,6 +24,16 @@ def predicates[T](list: List[T])(preds: List[T => Boolean]): List[T] = {
})
}
val l1 = (1 to 10).toList
val f1 = ((a: Int) => a % 2 == 0)
val f2 = ((a: Int) => a > 5)
predicates(l1)(List(f1, f2)) // List(6, 8, 10)
val l2 = ("Hello Scala Echo").toList
val f4 = ((a: Char) => a == 'a' || a == 'e' || a == 'o')
val f3 = ((a: Char) => !a.isUpper)
predicates(l2)(List(f3, f4)) // List('e', 'o', 'a', 'a', 'o')
/*
// Better solution
def predicates[T](list: List[T])(preds: List[T => Boolean]): List[T] = {
@ -41,4 +51,5 @@ def fixedPoint(f: Int => Int): Int => Int = {
func
}
fixedPoint(x => x / 2 + 5)(40)
fixedPoint(x => if (x % 10 == 0) x else x + 1)(35) // 40
fixedPoint(x => x / 2 + 5)(20) // 10