Scala
Scala
Installation
Arch Linux
pacman -S scala
SDKMAN!
sdk install scala
Binary
# Download scala-{version}.tar.gz from https://github.com/lampepfl/dotty/
tar xzf scala-{version}.tar.gz
mv scala-{version} /usr/local
export PATH=/usr/local/scala-{version}:$PATH
Examples
Fibonacci Numbers
// Fibonacci Numbers
object fib_scala {
def fib(n: Int): Int =
if (n < 2)
n
else
fib(n - 2) + fib(n - 1)
/* slower
def fib(n: Int): Int = n match {
case 0 => 0
case 1 => 1
case _ => fib(n - 2) + fib(n - 1)
}
*/
def main(args: Array[String]) = {
// run as script
var n = Integer.parseInt(args(0))
// run as byte code
//var n = Integer.parseInt(args(1))
println(fib(n))
}
}
/*
example
run as script:
scala fib.scala 39
compile:
scalac fib.scala
run as byte code:
scala fib_scala 39
*/