String greeting = "Hello"; greeting = 42; // Error
var greeting = "Hello" // String is inferred greeting = 42 // Error
var greeting = 'Hello' // This is JavaScript greeting = 42 // Ok alert(greeting.length) // Runtime error: 42 doesn't have a length member
var greeting : Any = "Hello" greeting = 42 // Ok
def mystery(x) = 42 * x // Error def mystery(x : Int) = 42 * x // Ok
Int, then 42 * x is also
Intdef fac(x : Int) = if (x == 0) 1 else x * fac(x - 1)
facdef twice(f: (Int)=>Int, x : Int) = f(f(x)) twice(x => 42 * x, 3) // Ok, x : Int is inferred from context
List(1, 2, 3).filter((x) => x % 2 == 0)
List[A].filter (p : (A) => Boolean) : List[A] A is Int since List(1, 2, 3)
is a List[Int]f must be (Int)=>Booleanx must be Int() around a single inferred parameter
List(1, 2, 3).filter(x => x % 2 == 0) List(1, 2, 3).sort((x, y) => x > y) // need () with 2 or more parameters
_ for a parameter that only occurs once in the body
List(1, 2, 3).filter(_ % 2 == 0) List(1, 2, 3).sort(_ > _)
map and filter have as argument
a function (i.e. code)while loop has two arguments: a condition (code) and a
body (code)while in Scala?
Sure:
def While(cond: () => Boolean, body: () => Unit) {
if (cond()) {
body(); While(cond, body)
}
}
var x = 1
While (() => x < 10,
() => { println(x); x+= 1 })
{} for blocks without ()
=>def While(cond: => Boolean, body: => Unit) { // Not () =>
if (cond) { // NOTE: No () in call
body; While(cond, body)
}
}
() => are gone from the call:
While ({ x < 10 },
{ x += 1; println(x) })
( , ) in call. 
def mul(x : Int, y : Int) = x * y mul(3, 4) is 12 def mul2(x : Int)(y : Int) = x * y mul2(3)(4) is 12
mul2(3)? y : Int => 3 * yWhile Control Abstractiondef While(cond: => Boolean)(body: => Unit) {
if (cond) {
body; While(cond)(body)
}
}
While (x < 20) { x += 1; println(x) }
While {x < 20} also worksPrintWriter writer = new PrintWriter("foo.txt");
try {
writer.println(line1);
writer.println(line2);
...
} finally {
writer.close(); // Make sure this happens for any exception
}
DoWrite(new PrintWriter("foo.txt")) { writer =>
writer.println(line1);
writer.println(line2);
...
}

/:)(Hard--may
be easier after step 2)For each of these, first store the list in a variable
lst
val lst = List("Alpha", "Bravo", "Charlie", "Delta")
def max(lst : List[String], less : (String, String) => Boolean) = (lst.head /: lst.tail)((x, y) => if (less(x, y)) y else x)
Make a call to max that yields the longest string in a list
lst. Use _ for the string parameters in your
less function.
def max[T](lst : List[T], less : (T, T) => Boolean) = (lst.head /: lst.tail)((x, y) => if (less(x, y)) y else x)
What happens when you call max(lst, _ < _)?
max[T] function, exactly like mul2 above. What is
the code for your revised function? What happens when you call
max2(lst)(_ < _)?
(Why does this work? Now the Scala type inferencer knows that
T must be String after processing
max2(lst).)
DoWriteDoWrite control abstraction from the lecture.
The While example isn't a perfect model to follow:
While took two function parameters () =>
Boolean, () => UnitDoWrite has one parameter of type
PrintWriter and one parameter that is a functionWhat is the type of that function?
DoWrite(writer : PrintWriter)(body: the
type you deduced in step 1). Make a try/finally
block. In the inside of the try, simply call
body. DoWrite(new PrintWriter("abc.txt")) { writer =>
writer.println("Alpha")
writer.println("Bravo")
writer.println("Charlie")
}
Where is the file abc.txt located, and what is its
contents?
DoWriteDoWrite abstraction does its job.
Call
def test() {
val writer = new PrintWriter("def.txt")
writer.println("Delta")
writer.println("Echo")
throw new NullPointerException()
writer.println("Foxtrot")
}
What happens?
def.txt? What did you expect to be in it?DoWrite(new PrintWriter("def.txt")) { writer =>
writer.println("Delta");
throw new NullPointerException()
writer.println("Foxtrot");
}
What is in def.txt? What does that prove?