progs/lecture1.scala
changeset 314 21b52310bd8b
parent 313 dea46bdfd648
child 316 8b57dd326a91
--- a/progs/lecture1.scala	Tue Nov 05 00:41:02 2019 +0000
+++ b/progs/lecture1.scala	Wed Nov 06 00:36:45 2019 +0000
@@ -5,10 +5,13 @@
 // (their names should be lower case)
 //====================================
 
+//var z = 9
+//z = 10
 
 val x = 42
 val y = 3 + 4
 val z = x / y
+val x = 70
 
 // (you cannot reassign values: z = 9 will give an error)
 
@@ -41,6 +44,7 @@
 // ranges
 1 to 10
 (1 to 10).toList
+(1 to 10).toList.toString
 
 (1 until 10).toList
 
@@ -69,9 +73,9 @@
 List(1,2,3) == List(3,1,2)
 
 
-// this applies to "concrete" values...pretty much everything;
-// but for example you cannot compare functions (later),
-// and also not Arrays
+// this applies to "concrete" values...pretty much 
+// everything; but for example you cannot compare 
+// functions (later), and also not arrays
 
 Array(1) == Array(1)
 
@@ -97,6 +101,7 @@
 println(lst.mkString(", "))
 
 // some methods take more than one argument
+
 println(lst.mkString("{", ",", "}"))
 
 // (in this case .mkString can take no, one, 
@@ -166,7 +171,7 @@
 
 
 // type errors
-math.sqrt("64")
+math.sqrt("64".toDouble)
 
 // produces
 //
@@ -193,6 +198,15 @@
 // Function Definitions
 //======================
 
+def foo(s: String) : String = {
+  val tmp = s ++ s ++ s
+  val res = s ++ s
+  res
+}
+
+
+foo("test")
+
 def incr(x: Int) : Int = x + 1
 def double(x: Int) : Int = x + x
 def square(x: Int) : Int = x * x
@@ -205,15 +219,14 @@
 str(3)
 
 
-// The general scheme for a function: you have to give a type 
-// to each argument and a return type of the function
+// The general scheme for a function: you have to give a 
+// type to each argument and a return type of the function
 //
 //  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
-//    body 
+//    
 //  }
 
 
-//
 // BTW: no returns!!
 // "last" line (expression) in a function determines the 
 // result
@@ -250,7 +263,6 @@
 
 fact2(150)
 
-
 def fib(n: Int) : Int =
   if (n == 0) 1 else
     if (n == 1) 1 else fib(n - 1) + fib(n - 2)
@@ -276,13 +288,13 @@
 // For-Comprehensions (not For-Loops)
 //====================================
 
-
+(1 to 10).toList
 for (n <- (1 to 10).toList) yield { 
   square(n) + 1
 }
 
 for (n <- (1 to 10).toList; 
-     m <- (1 to 10).toList) yield m * n
+     m <- (1 to 10).toList) yield (m, n)
 
 
 // you can assign the result of a for-comprehension
@@ -296,15 +308,20 @@
 
 // the list/set/... can also be constructed in any 
 // other way
+
 for (n <- Set(10,12,4,5,7,8,10)) yield n * n
 
+for (n <- (1 to 10)) yield {
+  n * n  
+}
+
+if (1 == 2) "a" else "b"
 
 // with if-predicates / filters
 
 for (n <- (1 to 3).toList; 
      m <- (1 to 3).toList;
-     if (n + m) % 2 == 0;
-     if (n * m) < 2) yield (n, m)
+     if (n + m) % 2 == 0) yield (n, m)
 
 for (n <- (1 to 3).toList; 
      m <- (1 to 3).toList;
@@ -330,13 +347,15 @@
 // Functions producing multiple outputs
 //======================================
 
-def get_ascii(c: Char) : (Char, Int) = (c, c.toInt)
+def get_ascii(c: Char) : (Char, Int) = 
+  (c, c.toInt)
 
 get_ascii('a')
 
 
 // .maxBy, sortBy with pairs
-def get_length(s: String) : (String, Int) = (s, s.length) 
+def get_length(s: String) : (String, Int) = 
+  (s, s.length) 
 
 val lst = List("zero", "one", "two", "three", "four", "ten")
 val strs = for (s <- lst) yield get_length(s)
@@ -356,7 +375,8 @@
 
 for (n <- (1 to 10)) println(n)
 
-
+(1 to 10).toList
+(1 until 10).toList
 // BTW: a roundabout way of printing out a list, say
 val lst = ('a' to 'm').toList