--- a/progs/lecture1.scala Mon Nov 06 21:49:55 2023 +0000
+++ b/progs/lecture1.scala Fri Dec 08 00:54:36 2023 +0000
@@ -1,7 +1,7 @@
// Scala Lecture 1
//=================
-// - List, Sets, Strings, ...
+// - List, Sets, Ints, Strings, ...
// - Value assignments (val vs var)
// - How to define functions? (What is returned?)
// - If-Conditions
@@ -19,31 +19,14 @@
val x = 42
val y = 3 + 4
val z = x / y
-val x = 70
+val x = 0
println(z)
// (you cannot reassign values: z = 9 will give an error)
-//var z = 9
-//z = 10
-
-
-// Hello World
-//=============
-
-// an example of a stand-alone Scala file
-// (in the assignments you must submit a plain Scala script)
+var z = 9
+z = 10
-object Hello extends App {
- println("hello world")
-}
-
-// can then be called with
-//
-// $> scalac hello-world.scala
-// $> scala Hello
-//
-// $> java -cp /usr/local/src/scala/lib/scala-library.jar:. Hello
@@ -56,6 +39,7 @@
// picking an element in a list
val lst = List(1, 2, 3, 1)
+
lst(0)
lst(2)
@@ -84,7 +68,7 @@
// Equality in Scala is structural
//=================================
-val a = "Dave"
+val a = "Dave2"
val b = "Dave"
if (a == b) println("Equal") else println("Unequal")
@@ -159,7 +143,6 @@
"1,2,3,4,5".split(",").toList
"1,2,3,4,5".split(",3,").mkString("\n")
-"abcdefg".startsWith("abc")
// Types (see slide)
@@ -241,6 +224,36 @@
// }
+// > LENGTH OF LIST EXAMPLE
+def len(xs: List[Int], acc: Int) : Int = {
+ if (xs == Nil) acc
+ else foo(xs.tail, acc + 1)
+}
+
+def len(xs: List[Int]) : Int = foo(xs, 0)
+
+len(List(1,2,3,4,1))
+
+
+def len(xs: List[Int]) : Int = {
+ if (xs == Nil) 0
+ else (1 + len(xs.tail))
+}
+
+
+
+len(List(1,2,3,4,1))
+
+
+
+def len(xs: List[Int]) : Int = xs match {
+ case Nil => 0
+ case x :: xs => 1 + len(xs)
+}
+
+len(List(1,2,3,4,1))
+
+
// If-Conditionals
//=================
@@ -318,7 +331,7 @@
}
for (n <- (1 to 10).toList;
- m <- (1 to 5).toList) yield (n, m, n * m)
+ m <- (1 to 5).toList) yield (n, m)
// you can assign the result of a for-comprehension
@@ -327,8 +340,11 @@
for (n <- (1 to 10).toList;
m <- (1 to 10).toList) yield n * m
-println(mult_table.mkString)
-mult_table.sliding(10,10).mkString("\n")
+println(mult_table.mkString(","))
+mult_table.sliding(10,10).toList
+
+
+.mkString("\n")
// for-comprehensions also work for other
// collections
@@ -336,11 +352,17 @@
for (n <- Set(10,12,4,5,7,8,10)) yield n * n
for (n <- (1 to 10)) yield {
+
n * n
}
// with if-predicates / filters
+val xs = for (n <- (1 to 3).toList;
+ m <- (1 to 3).toList) yield (n,m)
+
+xs.filter{case (m, n) => (n + m) % 2 == 0}
+
for (n <- (1 to 3).toList;
m <- (1 to 3).toList;
if (n + m) % 2 == 0) yield (n, m)
@@ -350,7 +372,7 @@
val lst = List((1, 4), (2, 3), (3, 2), (4, 1))
-for ((m, n) <- lst) yield m + n
+` yield m + n
for (p <- lst) yield p._1 + p._2
@@ -369,9 +391,11 @@
// with only a side-effect (no list is produced),
// has no "yield"
-for (n <- (1 to 10).toList) println(n * n)
+val xs = for (n <- (1 to 10).toList) yield println(n * n)
-for (n <- (1 to 10).toList) yield n * n
+xs.tail
+
+val foo = for (n <- (1 to 10).toList) yield n * n
// BTW: a roundabout way of printing out a list, say
@@ -450,15 +474,21 @@
// - no mutable data-structures (no Arrays, no ListBuffers)
// But what the heck....lets try to count to 1 Mio in parallel
+//
+// requires
+// scala-cli --extra-jars scala- parallel-collections_3-1.0.4.jar
+
import scala.collection.parallel.CollectionConverters._
-var cnt = 0
+def test() = {
+ var cnt = 0
-for(i <- (1 to 100_000).par) cnt += 1
+ for(i <- (1 to 100_000)) cnt += 1
-println(s"Should be 100000: $cnt")
+ println(s"Should be 100000: $cnt")
+}
-
+test()
// Or
// Q: Count how many elements are in the intersections of