In the sqrt example, the user does not need to see the sqrtIter, isGoodEnough, and other auxillary functions. Thus, we can wrap them in a block under sqrt so that sqrt is the only visible function.
A block is delimited by braces. The last element in the block is an expression that defines its value. The definitions inside a block are only visible within the block and shadow definitions of the same names outside the block.
Definitions outside the block are clearly visible to the inside of a block so there is no need to redefine parameters (such as x for sqrt). Nesting method definitions makes code much more cleaner as well. Here is my code for sqrt:
def abs(x: Double): Double = if (x > 0) x else -xUnlike Java, Scala does not require a semicolon at the end of every line. However, you do need it if you want to put more than one statement on one line. This raises the issue of multiline statements. You could wrap them in parentheses or put the operator on the first line. For example:
def sqrt(x: Double): Double = {
def isGoodEnough(guess: Double): Boolean =
abs(guess * guess - x) / x < 0.001
def improveGuess(guess: Double): Double =
(guess + x / guess) / 2
def sqrtIter(guess: Double): Double =
if (isGoodEnough(guess)) guess
else sqrtIter(improveGuess(guess))
sqrtIter(1.0)
}
a. (long-expression1
+ long-expression2)
b. long-expression1 +
long-expression2
No comments:
Post a Comment