Sorry for the really bad title, but I couldn’t come up with a better one…
I am following Structure and interpretation of computer programs and am trying to do the example from section 1.1.7 about Netwon’s method of seccessive approximation for finding square roots.
I try to implement it in F# and i believe I’m pretty close to getting there, but probably There is some kind of syntactic problem.
Here is my code (I used linqpad, hence the Dump()), below follows the question
let square a = a * a
let average a b = (a + b)/2.0
let a = average 2.0 1.0
a.Dump()
let improve guess x = average guess x
let i = improve 2.0 1.0
i.Dump()
let goodEnough guess x = abs (x - square(guess)) < 0.001
let g = goodEnough 3.0 4.0
g.Dump()
let g2 = goodEnough 2.0 4.0
g2.Dump()
let rec sqrtIterator guess x =
if goodEnough guess x then guess
else sqrtIterator(improve(guess x) x)
let sqrt x = sqrtIterator 1.0 x
I get an error on the recursive call to sqrtIterator saying:
This expression was expected to have type float but here has type float -> float.
So it seems I am missing a parameter making it return a function taking one parameter, but I can not see whats wrong?
Changing
sqrtIterator(improve(guess x) x)tosqrtIterator (improve guess x) xwould solve the problem.From the first part of the expression in
sqrtIterator:if goodEnough guess x then guessthe type checker knows that
guessis afloatsincegoodEnoughhas already had typefloat -> float.The
elsebranch is expected to has typefloatas well. Consequently,sqrtIteratorhas typefloat -> float -> float.However, you provided
sqrtIterator(improve(guess x) x)in theelsebranch. The outermost parentheses indicate thatimprove(guess x) xis a single parameter which supposes to be afloat.Now
sqrtIterator(improve(guess x) x)returnsfloat -> floathence the above error message.