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> <hidden> Solution: <code scala> def apply(n: Int, f: Int => Int): Int = { f(n) } </code> </hidden> **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> <hidden> Solution: <code scala> def doubler(): Int => Int = { x => 2*x } </code> or <code scala> def doubler(): Int => Int = { def double(x: Int): Int = { 2*x } double } </code> </hidden> ===== 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> <hidden> Solution: <code scala> def foldWith (op: (Int,Int) => Int)(start: Int, stop: Int): Int = { def tail_fold(crt: Int, acc: Int): Int = { if crt == stop then acc else tail_fold(crt + 1, op(acc, crt)) } tail_fold(start + 1, start) } </code> </hidden> **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> <hidden> Solution: <code scala> def foldConditional(op: (Int,Int) => Int, p: Int => Boolean)(start: Int, stop: Int): Int = { def tail_fold(crt: Int, acc: Int): Int = { if crt == stop then { acc } else { if p(crt) then { tail_fold(crt + 1, op(acc, crt)) } else { tail_fold(crt + 1, acc) } } } if p(start) then { tail_fold(start + 1, start) } else { if start < stop then { foldConditional(op, p)(start + 1, stop) } else { 0 } } } </code> </hidden> **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> <hidden> Solution: <code scala> def foldMap(op: (Int,Int) => Int, f: Int => Int)(start: Int, stop: Int): Int = { def tail_aux(crt: Int, acc: Int): Int = { if crt == stop then { acc } else { tail_aux(crt + 1, op(acc, f(crt))) } } tail_aux(start + 1, f(start)) } </code> </hidden> **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> <hidden> Solution: <code scala> def sumSquares(n: Int): Int = foldMap(_+_, x => x*x)(1, n) </code> or <code scala> def sumSquares(n: Int): Int = foldMap((x, y) => x + y, x => x*x)(1, n) </code> </hidden> **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> <hidden> Solution: <code scala> def hasDivisor(k: Int, start: Int, stop: Int): Boolean = foldMap(_ & _, _ % k)(start, stop) == 0 </code> or <code scala> def hasDivisor(k: Int, start: Int, stop: Int): Boolean = foldMap((x, y) => x & y, x => x % k)(start, stop) == 0 </code> </hidden> **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> <hidden> Solution: <code scala> def integrate(f: Double => Double)(start: Double, stop: Double): Double = { def aux(crt: Double, acc: Double): Double = { if crt >= stop then acc else aux(crt + 0.01, acc + (f(crt) + f(crt + 0.01))*0.01/2) } aux(start, 0) } </code> </hidden> ===== 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> <hidden> Solution: <code scala> def multiply(x:Int): Int => Int = { def multiply_aux(y: Int): Int = x * y multiply_aux } </code> </hidden> **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> <hidden> Solution: <code scala> def compare(x: Int) = { def comapre_aux(y: Int) = { def compare_aux2(z: Int) = { if x > y && x > z then x else if y > x && y > z then y else z } compare_aux2 } comapre_aux } </code> </hidden> **2.3.3** Modify the function from **3.2.1** so that it's uncurry <hidden> Solution: <code scala> def foldWith (op: (Int,Int) => Int, start: Int, stop: Int): Int = { def tail_fold(crt: Int, acc: Int): Int = { if crt == stop then acc else tail_fold(crt + 1, op(acc, crt)) } tail_fold(start + 1, start) } </code> </hidden> ===== 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 <hidden> Solution: <code scala> def f(x: Int): Boolean = x <= 20 </code> </hidden> **2.4.2** Write a characteristic functions for the numbers between 2 parameters <hidden> Solution: <code scala> def range_f(start: Int, stop: Int)(x: Int): Boolean = x >= start && x <= stop </code> </hidden> **2.4.3** Write a function that return the characteristic function of the reunion of two sets <hidden> Solution: <code scala> def reunion(s1: Int => Boolean, s2: Int => Boolean)(x: Int): Boolean = s1(x) || s2(x) </code> </hidden> **2.4.4** Write a function that return the characteristic function of the intersection of two sets <hidden> Solution: <code scala> def intersection(s1: Int => Boolean, s2: Int => Boolean)(x: Int): Boolean = s1(x) && s2(x) </code> </hidden>