Instructor:
How to combine multiple programming paradigms in a single language?
false || true
1 + 2
("hello" + " " + "world").length
val dir = java.io.File ("/tmp")dir.listFiles.filter (f => f.isDirectory && f.getName.startsWith ("c"))
5:Int
Int
5.toDouble
scala.Int
5.+ (6)
scala.runtime.RichInt
5.max (6)
e1.f(e2)
e1 f e2
5 + 6
5 max 6
def f () = 5 - "hello" // rejected by type checker
java.lang.Object
scala.AnyRef
var x = 10 // declare and initialize xx = 11 // assignment to x OK
val x = 10 // declare and initialize xx = 11 // assignment to x fails// error: reassignment to val
int x = 10; // declare and initialize xx = 11; // assignment to x OK
final int x = 10; // declare and initialize xx = 11; // assignment to x fails// error: cannot assign a value to final variable x
const int x = 10; // declare and initialize xx = 11; // assignment to x fails// error: assignment of read-only variable ‘x’
int f() { s_1; s_2; ... return e_n;}
def f() : Int = e_1 e_2 ... e_nend f
Also allowed:
def f() : Int = { e_1; e_2; ... return e_n;}
def plus (x:Int, y:Int) : Int = x + ydef times (x: Int, y:Int) = x * y
def fact(n: Int) : Int = if n <= 1 then 1 else n * fact (n - 1)end fact
For-expressions (non-recursive)
Range generator and iterator
def fact(n: Int) : Int = var result = 1 for i <- 1 to n /* by 1 */ do result *= i resultend fact
Collection iterators
def sum(xs: List[Int]) : Int = var result = 0 for n <- xs do result += n resultend sum
scala.collection
scala.collection.immutable
scala.collection.mutable
java.util
Array[Int]
Tuples: fixed number of immutable heterogeneous data items
val p = (5, "hello")// val p : (Int, String) = (5, "hello")val x = p(0)// val x : Int = p(0)val q = 5 -> "hello" // alternate syntax// val q : (Int, String) = (5, "hello")
public class Pair<X,Y> { final X x; final Y y; public Pair (X x, Y y) { this.x = x; this.y = y; }}Pair<Integer, String> p = new Pair<> (5, "hello");int x = p.x;
#include <tuple>auto p = std::make_tuple(5, std::string("hello"));// std::tuple<int, std::string> p = std::make_tuple(5, std::string("hello"))auto x = std::get<0>(p)// int x = std::get<0>(p)
Lists: variable number of immutable homogeneous data items
List constructors
List (11, 21, 30+1, 82/2)11 :: 21 :: (30+1) :: (82/2) :: Nil
Scala's :: is an infix operator to construct lists (pronounced "cons")
::
val xs = 11 :: 21 :: 31 :: 41 :: Nil// cons is right-associative - here it is with explicit parenthesesval ys = 11 :: (21 :: (31 :: (41 :: Nil)))// method-call style, not encouraged!val zs = Nil.::(41).::(31).::(21).::(11)
head
tail
val xs = List(11, 21, 31, 41)val x = xs.head // x == 11val ys = xs.tail // ys == List(21, 31, 41)
val x = 10
val x: Int = 10
var x = 10
(1, "hello")
1 -> "hello"
List(1,2,3)
1 :: 2 :: 3 :: Nil
def fact(n: Int) : Int = if n <= 1 then 1 else n * fact (n - 1) fact(3)--> if n <= 1 then 1 else n * fact (n - 1) // where n=3--> if 3 <= 1 then 1 else 3 * fact (3 - 1)--> if false then 1 else 3 * fact (3 - 1)--> 3 * fact (3 - 1)--> 3 * fact (2)--> 3 * (if n <= 1 then 1 else n * fact (n - 1)) // where n=2--> 3 * (if 2 <= 1 then 1 else 2 * fact (2 - 1))--> 3 * (if false then 1 else 2 * fact (2 - 1))--> 3 * (2 * fact (2 - 1))--> 3 * (2 * fact(1))--> 3 * (2 * (if n <= 1 then 1 else n * fact (n - 1))) // where n=1--> 3 * (2 * (if 1 <= 1 then 1 else 1 * fact (1 - 1)))--> 3 * (2 * (if true then 1 else 1 * fact (1 - 1)))--> 3 * (2 * 1)--> 3 * 2--> 6
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-cubes fa-stack-1x fa-inverse"></i></span> Structured Data - Tuples: fixed number of heterogeneous items `(1, "hello")` * Lists: variable number of homogeneous items<br/> `List(1, 2, 3)` or `1 :: 2 :: 3 :: Nil` * Immutable and mutable variants * Pattern matching to decompose structured data into its components ---
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-cubes fa-stack-1x fa-inverse"></i></span> Mutability: Variable vs. Data - Variable mutability is different from data mutability * Java _mutable linked list_ by default ```java List<Integer> xs = new List<> (); // mutable variable, mutable list final List<Integer> ys = xs; // immutable variable, mutable list xs.add (4); ys.add (5); // list is mutable through both references xs = new List<> (); // reference is mutable ys = new List<> (); // fails; reference is immutable * Scala _immutable linked list_ by default ```scala var xs = List (4, 5, 6) // mutable variable, immutable list val ys = xs // immutable variable, immutable list xs (1) = 7; ys (1) = 3 // fails; list is immutable xs = List (0) // reference is mutable ys = List () // fails; reference is immutable ``` ---