Scala 2.7.7-final

Version of implementation Scala of programming language Scala

Version of Scala, released on 28 October 2009.

Examples:

Hello, World! - Scala (141):

object Main {
    def main(args: Array[String]) {
        println("Hello, World!")
    }
}

Factorial - Scala (142):

This example uses recursive factorial definition.

object Factorial {
    def factorial(n: Int): Long = 
        if (n == 0) 1 
               else n * factorial(n - 1)
    def main(args: Array[String]) {
        for {i <- List.range(0, 17)} 
            yield { println(i + "! = " + factorial(i)) }
    }
}

Factorial - Scala (143):

This example uses iterative factorial definition.

object Factorial {
    def main(args: Array[String]) {
        var f = BigInt(1)
        format("0! = %s\n", f)
        for {i <- 1 to 16} {
             f *= i;
             format("%s! = %s\n", i, f)
        }
    }
}

Fibonacci numbers - Scala (144):

This example uses recursive definition of Fibonacci numbers.

object Fibonacci {
    def fibonacci(n: Int): Int = 
        if (n < 3) 1 
              else fibonacci(n - 1) + fibonacci(n - 2)
    def main(args: Array[String]) {
        for {i <- List.range(1, 17)} 
            yield { print(fibonacci(i) + ", ") }
        println("...")
    }
}

Fibonacci numbers - Scala (145):

This example shows the usage of lazy evaluations and infinite lists in Scala. Infinite list of Fibonacci numbers is defined using functions .zip and .tail in the same way as in Haskell example.

lazy val fib: Stream[Int] = Stream.cons(1, Stream.cons(1, fib.zip(fib.tail).map(p => p._1 + p._2)))
fib.take(16).print

Quadratic equation - Scala (230):

This example expands the interactive one with variables input.

import java.io.{BufferedReader, InputStreamReader}
 
object Main {
    def main(args: Array[String]) {
        var stdin = new BufferedReader(new InputStreamReader(System.in));
        var A = augmentString(stdin.readLine()).toInt;
        var B = augmentString(stdin.readLine()).toInt;
        var C = augmentString(stdin.readLine()).toInt;
        solve(A,B,C);
    }
    def output(real: Double, imag: Double): String = 
        if (imag == 0) ""+real
                  else "("+real+","+imag+")"
 
    def solve(A: Int, B: Int, C: Int)
    {   if (A == 0) print("Not a quadratic equation.")
        else
        {   def D = B*B - 4*A*C;
            if (D == 0) print("x = "+output(-B/2.0/A, 0));
            else if (D > 0) 
                      print("x1 = "+output((-B+Math.sqrt(D))/2.0/A, 0)+"\nx2 = "+output((-B-Math.sqrt(D))/2.0/A, 0));
                 else print("x1 = "+output(-B/2/A, Math.sqrt(-D)/2.0/A)+"\nx2 = "+output(-B/2/A, -Math.sqrt(-D)/2.0/A));
        }
    }
}