This is an old revision of the document!


Lab 3. 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

3.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 = {
   ???
}

Click to display ⇲

Click to hide ⇱

Solution:

def apply(n: Int, f: Int => Int): Int = {
   f(n)
}

3.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 = {
   ???
}

Click to display ⇲

Click to hide ⇱

Solution:

def doubler(): Int => Int = {
   x => 2*x
}

or

def doubler(): Int => Int = {
  def double(x: Int): Int = {
    2*x
  }
  double
}

3.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  = ???
  ??
}

Click to display ⇲

Click to hide ⇱

Solution:

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)
}

3.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 = ???

Click to display ⇲

Click to hide ⇱

Solution:

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
    }
  }
}

3.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 = ???

Click to display ⇲

Click to hide ⇱

Solution:

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))
}

3.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 = ???

Click to display ⇲

Click to hide ⇱

Solution:

def sumSquares(n: Int): Int = foldMap(_+_, x => x*x)(1, n)

or

def sumSquares(n: Int): Int = foldMap((x, y) => x + y, x => x*x)(1, n)

3.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 = ???

Click to display ⇲

Click to hide ⇱

Solution:

def hasDivisor(k: Int, start: Int, stop: Int): Boolean = foldMap(_ & _, _ % k)(start, stop) == 0

or

def hasDivisor(k: Int, start: Int, stop: Int): Boolean = foldMap((x, y) => x & y, x => x % k)(start, stop) == 0

3.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 = ???

Click to display ⇲

Click to hide ⇱

Solution:

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)
}

3.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

Click to display ⇲

Click to hide ⇱

Solution:

def multiply(x:Int): Int => Int = {
  def multiply_aux(y: Int): Int = x * y
  multiply_aux
}

3.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
}

Click to display ⇲

Click to hide ⇱

Solution:

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
}

3.3.3 Modify the function from 3.2.1 so that it's uncurry

Click to display ⇲

Click to hide ⇱

Solution:

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)
}

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 

3.4.1 Write a characteristic functions for the numbers below 20

Click to display ⇲

Click to hide ⇱

Solution:

def f(x: Int): Boolean = x <= 10

3.4.2 Write a characteristic functions for the numbers between 2 parameters

Click to display ⇲

Click to hide ⇱

Solution:

def range_f(start: Int, stop: Int)(x: Int): Boolean = x >= start && x <= stop

3.4.3 Write a function that return the characteristic function of the reunion of two sets

Click to display ⇲

Click to hide ⇱

Solution:

def reunion(s1: Int => Boolean, s2: Int => Boolean)(x: Int): Boolean = s1(x) || s2(x)

3.4.4 Write a function that return the characteristic function of the intersection of two sets

Click to display ⇲

Click to hide ⇱

Solution:

def intersection(s1: Int => Boolean, s2: Int => Boolean)(x: Int): Boolean = s1(x) && s2(x)