From d0e2bd48fbde69c1a4eb814d5e8094fbc8485d55 Mon Sep 17 00:00:00 2001
From: Matt Bovel <matthieu.bovel@epfl.ch>
Date: Mon, 11 Oct 2021 03:00:02 +0200
Subject: [PATCH] Add exercises/solution-02.md

---
 exercises/solution-02.md | 100 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 100 insertions(+)
 create mode 100644 exercises/solution-02.md

diff --git a/exercises/solution-02.md b/exercises/solution-02.md
new file mode 100644
index 0000000..75ee2b7
--- /dev/null
+++ b/exercises/solution-02.md
@@ -0,0 +1,100 @@
+# Exercise Session 2, Solutions
+
+## Question 1
+
+```scala
+def flip(f: (Double, Int) => Boolean): (Int, Double) => Boolean =
+  (x1: Int, x2: Double) => f(x2, x1)
+```
+
+## Question 2
+
+### Question 2.1
+
+```scala
+val id: Int => Int = (x: Int) => x
+```
+
+### Question 2.2
+
+```scala
+def compose(f: Int => Int, g: Int => Int): Int => Int =
+  (x: Int) => f(g(x))
+```
+
+`compose(id, f)(k)` evaluates to `f(k)`, as does `compose(f, id)(k)`.
+
+More generally (see ["Identity Function" on Wikipedia](https://en.wikipedia.org/wiki/Identity_function#Algebraic_properties)):
+
+> If f: M → N is any function, then we have f ∘ id<sub>M</sub> = f = id<sub>N</sub> ∘ f.
+
+### Question 2.3
+
+```scala
+def repeated(f: Int => Int, n: Int): Int => Int =
+  if n == 0 then identity
+  else compose(f, repeated(f, n-1))
+```
+
+## Question 3
+
+### Question 3.1
+
+```scala
+def curry2(f: (Double, Int) => Boolean): Double => (Int => Boolean) =
+  (x1: Double) => (x2: Int) => f(x1, x2)
+```
+
+### Question 3.2
+
+```scala
+def uncurry2(f: Double => Int => Boolean): (Double, Int) => Boolean =
+  (x1: Double, x2: Int) => f(x1)(x2)
+```
+
+## Question 4
+
+```scala
+import scala.annotation.tailrec
+
+def fixedPoint(f: Int => Int): Int => Int =
+  @tailrec
+  def rec(x: Int): Int =
+    val y = f(x)
+    if x == y then x else rec(y)
+  rec
+```
+
+- `fixedPoint(x => x/2)(4)` returns `0`.
+- `fixedPoint(id)(123456)` returns `123456`.
+- `fixedPoint(x => x + 1)(0)` does not terminate.
+- `fixedPoint(x => if (x % 10 == 0) x else x + 1)(35)` returns `40`.
+- `fixedPoint(x => x / 2 + 5)(20)` returns `10`.
+
+## Question 5
+
+### Question 5.1
+
+```scala
+def sum(a: Int, b: Int)(f: Int => Int): Int =
+  @tailrec
+  def loop(i: Int, acc: Int): Int =
+    if i > b then acc
+    else loop(i + 1, acc + f(i))
+  
+  loop(a, 0)
+```
+
+### Question 5.2
+
+```scala
+def quadratic(c: Int): Int => Int =
+  (x: Int) => (x - c) * (x - c)
+```
+
+### Question 5.3
+
+```scala
+def quad3Integrate(a: Int, b: Int): Int =
+  sum(a, b - 1)(quadratic(3))
+```
-- 
GitLab