SML中的代码逻辑错误

时间:2013-10-30 00:37:32

标签: smlnj

我在cornell课程中使用以下代码作为平方根,但是代码返回错误的值,代码是

fun squareRoot(x: real): real =
  let
    val delta = 0.0001
    fun goodEnough(guess: real): bool =
      if(abs(guess*guess - x) < delta) then true else false
    fun improve(guess: real): real =
      (guess + x/guess) / 2.0
    fun tryGuess(guess: real): real =
      if goodEnough(guess) then guess
      else tryGuess(improve(guess))
  in
    tryGuess(1.0)
  end

执行时

 use "squareroot1.sml";
[opening squareroot1.sml]
val squareRoot = fn : real -> real
val it = () : unit
- squareroot 9.0;
val it = 1.0 : real
-

它总是返回1.0,因为你可以告诉root为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

调用该函数时会出现拼写错误,您需要将squareroot更改为squareRoot