Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created October 5, 2013 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akimboyko/6844680 to your computer and use it in GitHub Desktop.
Save akimboyko/6844680 to your computer and use it in GitHub Desktop.
Functional Programming Principles in Scala week #02 Confusing sample with λ expression
def isCloseEnough(x : Double, y : Double) = {
val tolerance = 0.0001
Math.abs((x - y) / 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 + guess) / 2)
}
iterate(firstGuess)
}
def sqrt(x : Double) = fixedPoint(y => x / y)(1)
sqrt(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment