Scala Higher Order Function Little Confused

advertisements

I was running the below Scala code in Worksheet:

package src.com.sudipta.week2.coursera
import scala.math.abs
import scala.annotation.tailrec

object FixedPoint {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet

  val tolerance = 0.0001                          //> tolerance  : Double = 1.0E-4
  def isCloseEnough(x: Double, y: Double): Boolean = {
    abs((x - y) / x) / x < tolerance
  }                                               //> isCloseEnough: (x: Double, y: Double)Boolean
  def fixedPoint(f: Double => Double)(firstGuess: Double): Double = {
    @tailrec
    def iterate(guess: Double): Double = {
      val next = f(guess)
      if (isCloseEnough(guess, next)) next
      else iterate(next)
    }
    iterate(firstGuess)
  }                                               //> fixedPoint: (f: Double => Double)(firstGuess: Double)Double

  def myFixedPoint = fixedPoint(x => 1 + x / 2)(1)//> myFixedPoint: => Double
  myFixedPoint                                    //> res0: Double = 1.999755859375

  def squareRoot(x: Double) = fixedPoint(y => (y + x / y) / 2)(1)
                                                  //> squareRoot: (x: Double)Double
  squareRoot(2)                                   //> res1: Double = 1.4142135623746899

  def calculateAverate(f: Double => Double)(x: Double) = (x + f(x)) / 2
                                                  //> calculateAverate: (f: Double => Double)(x: Double)Double
  def myNewSquareRoot(x: Double): Double = fixedPoint(calculateAverate(y => x / y))(1)
                                                  //> myNewSquareRoot: (x: Double)Double
  myNewSquareRoot(2)                              //> res2: Double = 1.4142135623746899
}

What is making me confused are:

  • Scala worksheet is showing below for my fixedPoint function

fixedPoint: (f: Double => Double)(firstGuess: Double)Double

What is this? Is this function type/ function definition or I am missing the term? Basically how I can explain this function in English?

  • Scala worksheet is showing below for my calculateAverate function

calculateAverate: (f: Double => Double)(x: Double)Double

But it looks to me the return type of my function is Double, but I was expecting Double => Double. The reason is I am going to use this with fixedPoint which expects Double => Double like below:

def myNewSquareRoot(x: Double): Double = fixedPoint(calculateAverate(y => x / y))(1)

Please help me to understand Higher Order Function / Currying more clearly. Thanks in advance.


The function

def fixedPoint (f: Double => Double)(firstGuess: Double):Double

is a function definition which takes two arguments:

  1. a function "f" which takes a parameter of type Double which returns a Double and
  2. a Double value named "firstGuess"

and returns a Double. The separation of the single parameters into their own parenthesis groups allows the function to be used in currying:

def mySqrtCalc(x:Double)(firstGuess:Double):Double = {...}

val mySqrtFunc = mySqrtCalc(2.0) _

val v = mySqrtFunc(1) // parameter firstGuess is set to 1

Beside the ability for currying, this is equivalent to the uncurried version

def fixedPoint (f: Double => Double,firstGuess: Double):Double

which might look more familiar to you.

The result of the calculateAverate is a Double because you add up the result of applying the passed function f to x with x which gives you a Double because f is a function Double => Double.

You use calculateAverate method in a curried way in your myNewSquareRoot method body, by giving only the first of both parameters in first place and omitting the second one, which is taken from the from the outer fixedPoint method. Omitting the second parameter of calculateAverate gives you a method Double=>Double as it is required by the fixedPoint method.

You might insert some println(s"<methodname> value=$value") to observe the execution flow and understand the method call order.