Chapter 3, Exercise 3, Thinking in Scala
Today's exercise corresponds to this entry from the Lisp/Java version. Nothing new, though I suppose I should figure out whether "else if" chains work as expected.
package x;
object c3x3 {
def test ( testval: Int, target: Int ) = {
var result = 0;
if(testval > target) {
result = +1;
}
else {
if(testval < target) { result = -1 }
else { result = 0 }
}
result;
}
def test2 ( testval: Int, target: Int ) = {
if (testval > target) { 1 }
else {
if (testval < target) { -1 }
else { 0 }
}
}
def main ( args: Array[String] ) = {
Console.println(test(10, 5));
Console.println(test(5, 10));
Console.println(test(5, 5));
Console.println(test2(10, 5));
Console.println(test2(5, 10));
Console.println(test2(5, 5));
}
}
Hmm; they do seem to, though I'm not posting that version.
9:45:11 PM