This is an old revision of the document!


Scala @ PP

2.1. Writing functions 2.2. Function evaluation (order of parameters in function call matters !)

  1. using the to delay evaluation

2.3. Conditionals

  1. def f(x : Int) = if (x >= 0) x else -x
  2. boolean expressions (true, false, !b, &&, ||)
  3. the “by value” definition using val
  4. example using def and val against a loop

Newton square root estimation

  • Blocks - expressions in Scala
  • visibility of variables in blocks
  • local scoping (similar to let)
  • semicolons (optional)
  • tail recursion in Scala (only direct recursive calls are optimised @tailrec)
  • function types (f : Int ⇒ Int)
  • anonymous functions ((x: Int, y: Int) ⇒ x + y, x ⇒ x+x)
  • currying (def f(x: Int, y: Int)(z: Int):Int = x+y+z)
  • higher-order functions (mapReduce implementation)
  • classes and methods
class Rational(x: Int, y: Int) { // creates a Type as well as a constructor for the new type
    def a = x
    def b = y
}
 
val r = new Rational(1,2)
  • toString and @override