This is an old revision of the document!
Lab 2. High order functions
Objectives:
- implement and use higher-order functions. A higher-order function takes other functions as parameter or returns them
- implement curry and uncurry functions, and how they should be properly used (review lecture).
Create a new Scala worksheet to write your solutions
2.1 Intro. Functions as parameters
2.1.1 Write a function apply
that takes an integer and return the result of the applied function on the given integer. Start from the code stub below:
def apply(n: Int, f: Int => Int): Int = { ??? }
2.1.2 Write a function doubler
that returns a function that doubles the input it receives (an integer). Start from the code stub below:
def doubler(): Int => Int = { ??? }
2.2 Custom high order functions
2.2.1 Define the function foldWith
which uses an operation op
to reduce a range of integers to a value. For instance, given that op
is addition (+), the result of folding the range 1 to 3 will be 1+2+3=6. foldWith
should be curried (it will take the operation and return another function which expects the bounds).
def foldWith (op: (Int,Int) => Int)(start: Int, stop: Int): Int = { def tail_fold(crt: Int, acc: Int): Int = ??? ?? }
2.2.2 Define the function foldConditional
which extends foldWith
by also adding a predicate p: Int ⇒ Int
. foldConditional
will reduce only those elements of a range which satisfy the predicate.
def foldConditional(op: (Int,Int) => Int, p: Int => Boolean)(start: Int, stop: Int): Int = ???
2.2.3 Write a function foldMap
which takes values $ a_1, a_2, \ldots, a_k$ from a range and computes $ f(a_1)\;op\;f(a_2)\;op\;\ldots f(a_k)$ .
Use the apply
and foldWith
methods
def foldMap(op: (Int,Int) => Int, f: Int => Int)(start: Int, stop: Int): Int = ???
2.2.4 Write a function which computes $ 1 + 2^2 + 3^2 + \ldots + (n-1)^2 + n^2$ using foldMap
.
def sumSquares(n: Int): Int = ???
2.2.5 Write a function hasDivisor
which checks if a range contains a multiple of k. Use foldMap
and choose f
carefully.
def hasDivisor(k: Int, start: Int, stop: Int): Boolean = ???
2.2.6 We can compute the sum of an area defined by a function within a range a,b (the integral of that function given the range), using the following recursive scheme:
- if the range is small enough, we treat f as a line (and the area as a trapeze). It's area is $ (f(a) + f(b))(b-a)/2$ .
- otherwise, we compute the mid of the range, we recursively compute the integral from a to mid and from mid to b, and add-up the result.
Implement the function integrate
which computes the integral of a function f given a range:
def integrate(f: Double => Double)(start: Double, stop: Double): Double = ???
2.3 Curry vs Uncurry
2.3.1 Modify the function below so that it's curry and use it to calculate 5*3
def multiply(x:Int, y:Int): Int => x * y
2.3.2 Modify the function below so that it's curry and use it to compare 3 numbers and return the maximum
def compare(x: Int, y: Int, z: Int): Int = { if x > y && x > z then x else if y > x && y > z then y else z }
2.3.3 Modify the function from 3.2.1 so that it's uncurry
2.4 Characteristics set function
A characteristic set function is a function that describes a set of integers. Mathematically, it is defined as followed: f(a) is true if and only if a is in the set, false otherwise For example, for the set {1,5,6} can have the following function associated:
def f(x: Int): Boolean = x == 1 || x == 5 || x == 6
2.4.1 Write a characteristic functions for the numbers below 20
2.4.2 Write a characteristic functions for the numbers between 2 parameters
2.4.3 Write a function that return the characteristic function of the reunion of two sets
2.4.4 Write a function that return the characteristic function of the intersection of two sets