I am new to scala and have taken the coursera course for the functional development of scala. The code snippet below explains currying
import math.abs
object exercise{
val tolerance = 0.0001
def isCloseEnough(x: Double, y: Double) = abs((x -y)/x)/x < tolerance
def fixedPoint(f: Double => Double)(firstGuess: Double) = {
def iterate(guess: Double):Double = {
val next = f(guess)
if ( isCloseEnough(guess, next)) next
else iterate(next)
}
iterate(firstGuess)
}
def averageDamp(f: Double => Double)(x: Double) = ( x + f(x))/2
def sqrt(x: Double) = fixedPoint( averageDamp(y => x/y))(1)
}
I can not understand the next part of the code
fixedPoint( averageDamp(y => x/y))(1)
I know that the averageDamp function takes 2 arguments (one is the function and the other is the value for x), but when it is called from fixedPoint, we do not pass the value of x. Therefore, I assumed that it creates a partial function that is sent back to sqrt, where the value of x is passed from the sqrt (x: Double) agrument. So I made the following function, which does not compile
def noIdea(x: Double) = averageDamp( y => x/y)
Can someone explain this to me?
source
share