Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 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: <code scala> def apply(n: Int, f: Int => Int): Int = { ??? } </code> **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: <code scala> def doubler(): Int => Int = { ??? } </code> ===== 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). <code scala> def foldWith (op: (Int,Int) => Int)(start: Int, stop: Int): Int = { def tail_fold(crt: Int, acc: Int): Int = ??? ?? } </code> **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. <code scala> def foldConditional(op: (Int,Int) => Int, p: Int => Boolean)(start: Int, stop: Int): Int = ??? </code> **2.2.3** Write a function ''foldMap'' which takes values $math[a_1, a_2, \ldots, a_k] from a range and computes $math[f(a_1)\;op\;f(a_2)\;op\;\ldots f(a_k)]. Use the ''apply'' and ''foldWith'' methods <code scala> def foldMap(op: (Int,Int) => Int, f: Int => Int)(start: Int, stop: Int): Int = ??? </code> **2.2.4** Write a function which computes $math[1 + 2^2 + 3^2 + \ldots + (n-1)^2 + n^2] using ''foldMap''. <code scala> def sumSquares(n: Int): Int = ??? </code> **2.2.5** Write a function ''hasDivisor'' which checks if a range contains a multiple of k. Use ''foldMap'' and choose ''f'' carefully. <code scala> def hasDivisor(k: Int, start: Int, stop: Int): Boolean = ??? </code> **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 $math[(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: <code scala> def integrate(f: Double => Double)(start: Double, stop: Double): Double = ? ?? </code> ===== 2.3 Curry vs Uncurry ===== **2.3.1** Modify the function below so that it's curry and use it to calculate ''5*3'' <code scala> def multiply(x:Int, y:Int): Int => x * y </code> **2.3.2** Modify the function below so that it's curry and use it to compare 3 numbers and return the maximum <code scala> 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 } </code> **2.3.3** Modify the function from **2.2.1** so that i t'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: <code scala> def f(x: Int): Boolean = x == 1 || x == 5 || x == 6 </code> **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