From cdede53bbb931abab270620f81eea057164c5fc6 Mon Sep 17 00:00:00 2001
From: Olivier Blanvillain <olivier.blanvillain@gmail.com>
Date: Wed, 11 Sep 2019 17:08:16 +0200
Subject: [PATCH] Add recitation session 1

---
 recitation-sessions/recitation-session-1.md | 59 +++++++++++++++++++++
 1 file changed, 59 insertions(+)
 create mode 100644 recitation-sessions/recitation-session-1.md

diff --git a/recitation-sessions/recitation-session-1.md b/recitation-sessions/recitation-session-1.md
new file mode 100644
index 0000000..2c89530
--- /dev/null
+++ b/recitation-sessions/recitation-session-1.md
@@ -0,0 +1,59 @@
+# Recitation Session 1
+
+We will work on tail recursion in this session.
+
+## Exercise 1: Factorial
+
+Recall the factorial function that you saw in class
+
+```scala
+def factorial(n: Int): Int = if (n <= 0) 1 else n * factorial(n - 1)
+```
+
+Define a tail recursive version of it
+
+```scala
+def factorial(n: Int): Int = fact(n, 1)
+@tailrec
+def fact(n: Int, acc: Int): Int = ???
+```
+
+What would be the advantage of making `fact` an inner function to `factorial`?
+
+## Exercise 2: Sum of elements on a list
+
+Define a function that takes a list of integers and sums them. You can use the functions `head`, `tail`, and `isEmpty` on lists, as you have seen for your homework.
+
+```scala
+def sumList(ls: List[Int]): Int = ???
+```
+
+Convert your definition into a tail-recursive one.
+
+## Exercise 3: Fast exponentiation
+
+Fast exponentiation is a technique to optimize the exponentiation of numbers:
+
+```
+b²ⁿ = (b²)ⁿ = (bⁿ)²
+b²ⁿ⁺¹ = b * b²ⁿ
+```
+
+Define a function that implements this fast exponentiation. Can you define a tail recursive version as well?
+
+```scala
+def fastExp(base: Int, exp: Int): Int = ???
+```
+
+## Exercise 4: Tail recursive Fibonacci
+
+Define a function that computes the nth Fibonacci number. Can you define a tail recursive version as well? The Fibonacci recurrence is given as follows:
+
+```
+fib(n) = 1 | n = 0, 1
+fib(n) = fib(n - 1) + fib(n - 2) | otherwise
+```
+
+```scala
+def fibonacci(n: Int): Int = ???
+```
-- 
GitLab