diff -r 86a456f8cb92 -r 135bf034ac30 progs/lecture1.scala --- a/progs/lecture1.scala Sat Sep 23 23:49:44 2023 +0100 +++ b/progs/lecture1.scala Wed Nov 01 15:01:32 2023 +0000 @@ -5,20 +5,11 @@ // - Value assignments (val vs var) // - How to define functions? (What is returned?) // - If-Conditions - -val tmp = 0 -val result = !(tmp == 0) -val result = if (tmp == 0) true else false - -// expressions (if (tmp == 0) true else false) // - For-Comprehensions (guards, with/without yield) +// - String-Interpolations // // -// - Options -// - Higher-Order Functions (short-hand notation) -// - maps (behind for-comprehensions) -// - Pattern-Matching -// - String-Interpolations + // Value assignments // (their names should be lower case) @@ -29,13 +20,14 @@ val y = 3 + 4 val z = x / y val x = 70 -print(z) +println(z) // (you cannot reassign values: z = 9 will give an error) //var z = 9 //z = 10 + // Hello World //============= @@ -92,7 +84,7 @@ // Equality in Scala is structural //================================= -val a = "Dave2" +val a = "Dave" val b = "Dave" if (a == b) println("Equal") else println("Unequal") @@ -151,8 +143,8 @@ 1F // a Float 1D // a Double -// useful list methods on lists -//============================== +// useful methods for lists +//========================= List(1,2,3,4).length List(1,2,3,4).reverse @@ -200,14 +192,14 @@ val name : String = "bob" // type errors -math.sqrt("64".toDouble) +math.sqrt("64") // produces // -// error: type mismatch; -// found : String("64") -// required: Double -// math.sqrt("64") +// Type Mismatch Error: +// Found : ("64" : String) +// Required: Double +// // Pairs/Tuples @@ -245,7 +237,7 @@ // type to each argument and a return type of the function // // def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = { -// +// .... // } @@ -253,12 +245,18 @@ // If-Conditionals //================= -// - Scala does not have a then-keyword +// - Scala used to not have a then-keyword // - !!both if-else branches need to be present!! def fact(n: Int) : Int = if (n == 0) 1 else n * fact(n - 1) + +// Scala 3 introduced if-then-else - maybe people +// desperately needed it +def fact(n: Int) : Int = + if n == 0 then 1 else n * fact(n - 1) + fact(5) fact(150) @@ -271,7 +269,6 @@ */ - def fib(n: Int) : Int = { if (n == 0) 1 else if (n == 1) 1 else fib(n - 1) + fib(n - 2) @@ -280,8 +277,6 @@ fib(9) - - //gcd - Euclid's algorithm def gcd(a: Int, b: Int) : Int = { @@ -306,8 +301,9 @@ else xs.sum / xs.length } +average(List(3,4,5)) average(List()) - +average(Nil) // For-Comprehensions (not For-Loops) @@ -377,12 +373,14 @@ for (n <- (1 to 10).toList) yield n * n + // BTW: a roundabout way of printing out a list, say val lst = ('a' to 'm').toList for (i <- (0 until lst.length)) println(lst(i)) -// Why not just? Why making your life so complicated? +// ...why not just the following? Why making your life +// so complicated? for (c <- lst) println(c) @@ -414,7 +412,7 @@ // Aside: concurrency -// scala -Yrepl-class-based -cp scala-parallel-collections_2.13-0.2.0.jar +// scala-cli --extra-jars scala-parallel-collections_3-1.0.4.jar for (n <- (1 to 10)) println(n) @@ -431,7 +429,7 @@ (end - start) / 1.0e9 } -val list = (1 to 1000000).toList +val list = (1L to 10_000_000L).toList time_needed(10, for (n <- list) yield n + 42) time_needed(10, for (n <- list.par) yield n + 42) @@ -456,9 +454,9 @@ var cnt = 0 -for(i <- (1 to 1000000).par) cnt += 1 +for(i <- (1 to 100_000).par) cnt += 1 -println(s"Should be 1 Mio: $cnt") +println(s"Should be 100000: $cnt") @@ -469,7 +467,7 @@ def count_intersection(A: Set[Int], B: Set[Int]) : Int = { var count = 0 - for (x <- A.par; if (B contains x)) count += 1 + for (x <- A.par; if B contains x) count += 1 count } @@ -488,44 +486,66 @@ count_intersection2(A, B) -//another bad example -def test() = { - var cnt = 0 - for(i <- (1 to 1000000).par) cnt += 1 - println(cnt) -} + +// String Interpolations +//======================= + +def cube(n: Int) : Int = n * n * n + +val n = 3 +println("The cube of " + n + " is " + cube(n) + ".") + +println(s"The cube of $n is ${cube(n)}.") -test() +// or even + +println(s"The cube of $n is ${n * n * n}.") + +// helpful for debugging purposes +// +// "The most effective debugging tool is still careful +// thought, coupled with judiciously placed print +// statements." +// — Brian W. Kernighan, in Unix for Beginners (1979) -// Regular Expressions (the built in ones) +def gcd_db(a: Int, b: Int) : Int = { + println(s"Function called with $a and $b.") + if (b == 0) a else gcd_db(b, a % b) +} -val s = """Any so-called "politician" should respect a vote.""" -print(s) +gcd_db(48, 18) -print("""foo""") +// you can also implement your own string interpolations -val reg = """\d+""".r +extension (sc: StringContext) { + def i(args: Any*): String = s"\t${sc.s(args:_*)}\n" + def l(args: Any*): String = s"${sc.s(args:_*)}:\n" +} -reg.findAllIn("bbbbaaabbbaaaccc").toList -reg.replaceAllIn("bbbbaaabbbaaaccc", "*") -reg.replaceAllIn("bbbb0aaa1bbba2aac3cc", "_") -reg.replaceAllIn("bbbb00aaa11bbba232aac33cc", "_") +// this allows you to write things like +i"add ${3+2}" +l"some_fresh_name" // Further Information //===================== +// We are going to use Scala 3 and the scala-cli repl (easier to use) +// +// https://scala-cli.virtuslab.org +// +// // The Scala homepage and general information is at // // http://www.scala-lang.org // http://docs.scala-lang.org // // -// It should be fairly easy to install the Scala binary and +// It should be fairly easy to install the scala-cli binary and // run Scala on the commandline. People also use Scala with -// Vim and Jedit. I currently settled on VS Code +// Vim and Jedit. I currently settled on Codium // // https://code.visualstudio.com // @@ -542,7 +562,7 @@ // Scala Library Docs //==================== // -// http://www.scala-lang.org/api/current/ +// https://dotty.epfl.ch/api/index.html // // Scala Tutorials // @@ -550,6 +570,6 @@ // // There are also a massive number of Scala tutorials on youtube // and there are tons of books and free material. Google is your -// friend. +// friend. Just make sure you follow newer material about Scala 3.