progs/lecture1.scala
changeset 494 253d1ccb65de
parent 481 e03a0100ec46
--- a/progs/lecture1.scala	Sun Sep 15 12:57:59 2024 +0100
+++ b/progs/lecture1.scala	Mon Jul 21 16:38:07 2025 +0100
@@ -1,6 +1,9 @@
 // Scala Lecture 1
 //=================
 
+println("Hello World")
+
+
 // - List, Sets, Ints, Strings, ... 
 // - Value assignments (val vs var)
 // - How to define functions? (What is returned?)
@@ -11,6 +14,7 @@
 //
 
 
+
 // Value assignments
 // (their names should be lower case)
 //====================================
@@ -19,13 +23,11 @@
 val x = 42
 val y = 3 + 4 
 val z = x / y
-val x = 0
+val x = 17 
 println(z)
 
 
 // (you cannot reassign values: z = 9 will give an error)
-var z = 9
-z = 10
 
 
 
@@ -33,12 +35,21 @@
 // Collections
 //=============
 
-List(1,2,3,1)
-Set(1,2,3,1)
+val ls = List("1","2","3","1")
+
+val ls2 = ls ::: ls
+
+val s1 = Set(1,2,3,1)
 
 // picking an element in a list
 val lst = List(1, 2, 3, 1)
 
+val lst = List(1, 2, 3, 1)
+
+val lst1 = 0 :: lst
+
+println(lst)
+println(lst1)
 
 lst(0)
 lst(2)
@@ -55,7 +66,7 @@
 List(1, 2, 3) ::: List(4, 5, 6)
 
 // also
-List(1, 2, 3) ++ List(3, 6, 5)
+List(1, 2, 3) ::: List(3, 6, 5)
 Set(1, 2, 3) ++ Set(3, 6, 5)
 
 // ranges
@@ -172,7 +183,7 @@
 // you can make the type of a value explicit
 val name = "bob"
 
-val name : String = "bob"
+val name : Int = "bob"
 
 // type errors
 math.sqrt("64")
@@ -202,20 +213,6 @@
 // Function Definitions
 //======================
 
-
-def incr(x: Int) : Int = x + 1
-def double(x: Int) : Int = x + x
-def square(x: Int) : Int = x * x
-
-def str(x: Int) : String = x.toString
-
-
-incr(3)
-double(4)
-square(6)
-str(3)
-
-
 // The general scheme for a function: you have to give a 
 // type to each argument and a return type of the function
 //
@@ -223,27 +220,37 @@
 //     ....
 //  }
 
-
-// > LENGTH OF LIST EXAMPLE
-def len(xs: List[Int], acc: Int) : Int = {
-   if (xs == Nil) acc
-   else foo(xs.tail, acc + 1)
+def average(ls: List[Int]) : Int = {
+  println(s"$ls")
+  val s = ls.sum
+  val l = ls.length
+  if (l == 0) 0 
+  else s / l
 }
 
-def len(xs: List[Int]) : Int = foo(xs, 0)
+average(List(1,2,3,4,56))
+def incr(x: Int) : Int = x + 1
+def double(x: Int) : Int = x + x
+def square(x: Int) : Int = x * x
+
+def str(x: Int) : String = x.toString
+
+incr(3)
+double(4)
+square(6)
+str(3)
+
+
+
+
+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 = {
-    if (xs == Nil) 0
-    else (1 + len(xs.tail))
-}    
-
-
-
-len(List(1,2,3,4,1))
-
 
 
 def len(xs: List[Int]) : Int = xs match {
@@ -322,9 +329,12 @@
 // For-Comprehensions (not For-Loops)
 //====================================
 
-val lst = (1 to 10).toList
-for (n <- lst) yield n * n 
+val lst = (1 to 10).toSet
+val result = for (n <- lst) yield {
+  n * n 
+}
 
+println(result)
 
 for (n <- lst) yield { 
   square(n) + double(n)
@@ -341,7 +351,7 @@
        m <- (1 to 10).toList) yield n * m
 
 println(mult_table.mkString(","))
-mult_table.sliding(10,10).toList
+mult_table.sliding(10,10).toList.mkString(",")
 
 
 .mkString("\n")
@@ -361,7 +371,6 @@
 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;
@@ -397,6 +406,7 @@
 
 val foo = for (n <- (1 to 10).toList) yield n * n
 
+println(for (n <- (1 to 10).toList)  print(n))
 
 // BTW: a roundabout way of printing out a list, say
 val lst = ('a' to 'm').toList
@@ -422,7 +432,12 @@
 def get_length(s: String) : (String, Int) = 
   (s, s.length) 
 
+def len(s: String) : Int = s.length
+
+
 val lst = List("zero", "one", "two", "three", "four", "ten")
+lst.sorted
+lst.sortBy(len(_))
 val strs = for (s <- lst) yield get_length(s)
 
 strs.sortBy(_._2)
@@ -476,7 +491,7 @@
 // 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
+// scala --extra-jars scala- parallel-collections_3-1.0.4.jar
 
 import scala.collection.parallel.CollectionConverters._
 
@@ -562,21 +577,18 @@
 // 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
+// We are going to use Scala 3. 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-cli binary and
+// It should be fairly easy to install the scala binary and
 // run Scala on the commandline. People also use Scala with 
-// Vim and Jedit. I currently settled on Codium
+// Vim and Jedit. I currently settled on Codium / VSCode
 //
+//   https://vscodium.com
 //   https://code.visualstudio.com
 //
 // There are also plugins for Eclipse and IntelliJ - YMMV.
@@ -584,7 +596,6 @@
 // running Scala applications (but do not blame me if you lose 
 // all what you typed in):
 //
-//   https://scalafiddle.io 
 //   https://scastie.scala-lang.org
 //
 //