我最近在 scala 并试图通过一些简单的 scala 工作表。
IDE 是 Intellij IDEA 社区版和操作系统 Ubuntu 12.04,sbt
已正确安装。
但它会引发错误 -error: not found: value
OI 无法理解为什么会这样:
代码:
object session {
1 + 2
def abs(x: Double) = if (x < 0) -x else x <== update this line
def sqrtIter(guess: Double, x: Double): Double =
if (isGoodEnough(guess, x)) guess
else sqrtIter(improve(guess, x), x)
def isGoodEnough(guess: Double, x: Double) =
abs(guess * guess - x) < 0.001
def improve(guess: Double, x: Double) =
(guess + x / guess) / 2
def sqrt(x: Double) = sqrtIter(1.0, x)
sqrt(2)
sqrt(4)
}
从屏幕右侧输出:
> res0: Int = 3
> <console>:8: error: not found: value isGoodEnough
if (isGoodEnough(guess, x)) guess else sqrtIter(improve(guess, x), x)
^
<console>:8: error: not found: value improve
if (isGoodEnough(guess, x)) guess else sqrtIter(improve(guess, x), x)
^
> <console>:8: error: not found: value abs
abs(guess * guess - x) < 0.001
^
> improve: (guess: Double, x: Double)Double
> <console>:7: error: not found: value sqrtIter
def sqrt(x: Double) = sqrtIter(1.0, x)
^
> <console>:8: error: not found: value sqrt
sqrt(2)
^
> <console>:8: error: not found: value sqrt
sqrt(4)
有什么建议么?