Note that there are some explanatory texts on larger screens.

plurals
  1. POScala Higher Order Function Little Confused
    primarykey
    data
    text
    <p>I was running the below Scala code in Worksheet:</p> <pre><code>package src.com.sudipta.week2.coursera import scala.math.abs import scala.annotation.tailrec object FixedPoint { println("Welcome to the Scala worksheet") //&gt; Welcome to the Scala worksheet val tolerance = 0.0001 //&gt; tolerance : Double = 1.0E-4 def isCloseEnough(x: Double, y: Double): Boolean = { abs((x - y) / x) / x &lt; tolerance } //&gt; isCloseEnough: (x: Double, y: Double)Boolean def fixedPoint(f: Double =&gt; Double)(firstGuess: Double): Double = { @tailrec def iterate(guess: Double): Double = { val next = f(guess) if (isCloseEnough(guess, next)) next else iterate(next) } iterate(firstGuess) } //&gt; fixedPoint: (f: Double =&gt; Double)(firstGuess: Double)Double def myFixedPoint = fixedPoint(x =&gt; 1 + x / 2)(1)//&gt; myFixedPoint: =&gt; Double myFixedPoint //&gt; res0: Double = 1.999755859375 def squareRoot(x: Double) = fixedPoint(y =&gt; (y + x / y) / 2)(1) //&gt; squareRoot: (x: Double)Double squareRoot(2) //&gt; res1: Double = 1.4142135623746899 def calculateAverate(f: Double =&gt; Double)(x: Double) = (x + f(x)) / 2 //&gt; calculateAverate: (f: Double =&gt; Double)(x: Double)Double def myNewSquareRoot(x: Double): Double = fixedPoint(calculateAverate(y =&gt; x / y))(1) //&gt; myNewSquareRoot: (x: Double)Double myNewSquareRoot(2) //&gt; res2: Double = 1.4142135623746899 } </code></pre> <p>What is making me confused are:</p> <ul> <li>Scala worksheet is showing below for my fixedPoint function</li> </ul> <blockquote> <p>fixedPoint: (f: Double => Double)(firstGuess: Double)Double</p> </blockquote> <p>What is this? Is this function type/ function definition or I am missing the term? Basically how I can explain this function in English?</p> <ul> <li>Scala worksheet is showing below for my calculateAverate function</li> </ul> <blockquote> <p>calculateAverate: (f: Double => Double)(x: Double)Double</p> </blockquote> <p>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:</p> <blockquote> <p>def myNewSquareRoot(x: Double): Double = fixedPoint(calculateAverate(y => x / y))(1)</p> </blockquote> <p>Please help me to understand Higher Order Function / Currying more clearly. Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload