From 25b7e6f493891fbc9730328778c864e684430061 Mon Sep 17 00:00:00 2001 From: Olivier Blanvillain <olivier.blanvillain@gmail.com> Date: Mon, 30 Sep 2019 08:56:11 +0200 Subject: [PATCH] Add recitation session 2 --- recitation-sessions/recitation-session-2.md | 108 ++++++++++++++++++++ recitation-sessions/solution-1.md | 35 +++++++ 2 files changed, 143 insertions(+) create mode 100644 recitation-sessions/recitation-session-2.md create mode 100644 recitation-sessions/solution-1.md diff --git a/recitation-sessions/recitation-session-2.md b/recitation-sessions/recitation-session-2.md new file mode 100644 index 0000000..fb814cd --- /dev/null +++ b/recitation-sessions/recitation-session-2.md @@ -0,0 +1,108 @@ +# Recitation Session 2 + +This week we will work on playing with functions as values. + +## Exercise 1. + +Define the function `flip`. It takes a function and returns the same function, but with the arguments flipped. + +```scala +def flip(f: (Int, Double) => Int): (Double, Int) => Int = ??? +``` + +## Exercise 2.1 + +Define the identity function for integers, which, given an `Int`, returns it + +```scala +val id: Int => Int = ??? +``` + +## Exercise 2.2 + +Define the compose function, that, given 2 functions `f`, `g`, returns a function that composes them, i.e., `f ∘ g`. + +```scala +def compose(f: Int => Int, g: Int => Int): Int => Int = ??? +``` + +What does `compose(id, f)(k)` evaluate to, for some function `f` and integer `k`? + +## Exercise 2.3 + +Define the function `repeated`, which takes a function and repeatedly applies it `n` times (`n ≥ 0`). + +```scala +def repeated(f: Int => Int, n: Int): Int => Int = ??? +``` + +_Hint_: What values should be returned by `repeated(x => x + 1, 0)` and `repeated(x => x + 1, 3)`? + +## Exercise 3.1 + +Define the function `curry2`, that curries a two arguments function. That is, `curry2(f) = f'` such that `f(x, y) == (f'(x))(y)` + +```scala +def curry2(f: (Int, Double) => Boolean): Int => (Double => Boolean) = ??? +``` + +_Hint_: what should `curry2((x, y) => x + y)(1.0)` return? + +## Exercise 3.2 + +Define the function `uncurry2`. It takes a curried function, and creates a two-argument function. + +```scala +def uncurry2(f: Int => Double => Boolean): (Int, Double) => Boolean = ??? +``` + +## Exercise 4. + +Write a function `fixedPoint` with the following signature: + +```scala +def fixedPoint(f: Int => Int): Int => Int +``` + +The function takes a function `f` and returns a function that maps an integer into the fixed point of f that is obtained by iterating `f` some finite number of times starting from the initial value. + +A value `x` is a fixed point of `f` if `f(x) == x`. + +For each of the following expressions, indicate whether it terminates, and if so, what is the value returned: + +- `fixedPoint(x => x/2)(4)` +- `fixedPoint(id)(123456)` +- `fixedPoint(x => x + 1)(0)` +- `fixedPoint(x => if (x % 10 == 0) x else x + 1)(35)` +- `fixedPoint((x: Int) => x / 2 + 5)(20)` + +## Exercise 5.1 + +Write the `sum` function with the following signature: + +```scala +def sum(a: Int, b: Int)(f: Int => Int): Int = ??? +``` + +Which returns the sum of `f(i)` where `i` ranges from `a` to `b`. + +_Bonus point_: Can your implementation be tail recursive ? + +## Exercise 5.2 + +Write the `quadratic` function with the following signature: + +```scala +def quadratic(c: Int): Int => Int = ??? +``` + +Which returns a function that takes an integer `x` as argument and returns `(x - c)²`. + +## Exercise 5.3 + +Using the above functions, define the function `quad3Integrate` which, given two integers `a` and `b`, computes the sum of `(i - 3)²` where `i` ranges from `a` to `b - 1`. + +```scala +def quad3Integrate(a: Int, b: Int): Int = ??? +val quad3Integrate: (Int, Int) => Int = ??? +``` diff --git a/recitation-sessions/solution-1.md b/recitation-sessions/solution-1.md new file mode 100644 index 0000000..d4dbac2 --- /dev/null +++ b/recitation-sessions/solution-1.md @@ -0,0 +1,35 @@ +# Recitation Session 1, Solutions + +## Exercise 3: Fast exponentiation + +```scala +def fastExp(base: Int, exp: Int): Int = { + require(exp >= 0) + + @tailrec + def go(base: Int, exp: Int, acc: Int): Int = { + if (exp == 0) + acc + else if ((exp % 2) != 0) + go(base, exp - 1, base * acc) + else + go(base * base, exp / 2, acc) + } + go(base, exp, 1) +} +``` + +## Exercise 4: Tail recursive Fibonacci + +```scala +def fibonacci(n: Int): Int = { + require(n >= 0) + + @tailrec + def go(k: Int, previous: Int, current: Int): Int = + if (k == n) current + else go(k + 1, current, previous + current) + + if (n == 0) 1 else go(1, 1, 1) +} +``` -- GitLab