progs/lecture1.scala
changeset 356 d1046d9d3213
parent 353 bb6074814a73
child 359 8aaf187d25f0
--- a/progs/lecture1.scala	Sun Nov 08 13:09:38 2020 +0000
+++ b/progs/lecture1.scala	Mon Nov 09 14:34:12 2020 +0000
@@ -41,6 +41,27 @@
 List(1,2,3,1)
 Set(1,2,3,1)
 
+// picking an element in a list
+val lst = List(1, 2, 3, 1)
+
+lst(0)
+lst(2)
+
+// head and tail
+lst.head
+lst.tail
+
+// some alterative syntax for lists
+
+Nil     // empty list
+
+1 :: 2 :: 3 :: Nil
+List(1, 2, 3) ::: List(4, 5, 6)
+
+// also
+List(1, 2, 3) ++ List(3, 6, 5)
+Set(1, 2, 3) ++ Set(3, 6, 5)
+
 // ranges
 1 to 10
 (1 to 10).toList
@@ -48,23 +69,10 @@
 
 (1 until 10).toList
 
-// picking an element in a list
-val lst = List(1, 2, 3, 1)
-lst(0)
-lst(2)
-
-// some alterative syntax for lists
-
-1 :: 2 :: 3 :: Nil
-List(1, 2, 3) ::: List(4, 5, 6)
-
-// also
-List(1, 2, 3) ++ List(4, 5, 6)
-
 
 // Equality in Scala is structural
 //=================================
-val a = "Dave"
+val a = "Dave2"
 val b = "Dave"
 
 if (a == b) println("Equal") else println("Unequal")
@@ -113,15 +121,18 @@
 List(1,2,3,1).toSet
 
 "hello".toList
-"hello".toList.tail
+"hello".toSet
 
 
 1.toDouble
 
-1L  // a long
-1F  // a float
+1   // an Int
+1L  // a Long
+1F  // a Float
+1D  // a Double
 
-// useful list methods
+// useful list methods on lists
+//==============================
 
 List(1,2,3,4).length
 List(1,2,3,4).reverse
@@ -164,7 +175,7 @@
 
 
 // you can make the type of a value explicit
-val name: String = "leo"
+val name = "bob"
 
 
 // type errors
@@ -195,14 +206,6 @@
 // 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
@@ -210,6 +213,7 @@
 
 def str(x: Int) : String = x.toString
 
+
 incr(3)
 double(4)
 square(6)
@@ -224,16 +228,6 @@
 //  }
 
 
-// BTW: no returns!!
-// "last" line (expression) in a function determines the 
-// result
-
-
-def silly(n: Int) : Int = {
-  if (n < 10) n * n
-  else n + n
-}
-
 
 // If-Conditionals
 //=================
@@ -280,6 +274,9 @@
 
 power(5, 5)
 
+// BTW: no returns!!
+// "last" line (expression) in a function determines the 
+// result
 
 
 // For-Comprehensions (not For-Loops)