--- a/progs/lecture1.scala Fri Nov 09 07:30:02 2018 +0000
+++ b/progs/lecture1.scala Thu Nov 15 03:35:38 2018 +0000
@@ -5,11 +5,11 @@
// (their names should be lower case)
//====================================
+
val x = 42
val y = 3 + 4
val z = x / y
-
// (you cannot reassign values: z = 9 will give an error)
@@ -57,11 +57,15 @@
val a = "Dave"
val b = "Dave"
-if (a == b) println("equal") else println("unequal")
+if (a == b) println("Equal") else println("Unequal")
Set(1,2,3) == Set(3,1,2)
List(1,2,3) == List(3,1,2)
+val n1 = 3 + 7
+val n2 = 5 + 5
+
+n1 == n2
// this applies to "concrete" values;
// you cannot compare functions
@@ -77,13 +81,14 @@
val lst = List(1,2,3,1)
+
println(lst.toString)
-println(lst.mkString("\n"))
+println(lst.mkString(","))
println(lst.mkString(", "))
// some methods take more than one argument
-println(lst.mkString("[", ",", "]"))
+println(lst.mkString("{", ",", "}"))
@@ -92,7 +97,7 @@
List(1,2,3,1).toString
List(1,2,3,1).toSet
-"hello".toList
+"hello".toList.tail
1.toDouble
@@ -108,6 +113,7 @@
List(1,2,3,4,3).indexOf(3)
"1,2,3,4,5".split(",").mkString("\n")
+"1,2,3,4,5".split(",").toList
"1,2,3,4,5".split(",3,").mkString("\n")
"abcdefg".startsWith("abc")
@@ -155,6 +161,7 @@
def double(x: Int) : Int = x + x
def square(x: Int) : Int = x * x
+def str(x: Int) : String = x.toString
square(6)
@@ -172,8 +179,8 @@
//
def silly(n: Int) : Int = {
- n * n
- n + n
+ if (n < 10) n * n
+ else n + n
}
@@ -211,8 +218,13 @@
//gcd - Euclid's algorithm
-def gcd(a: Int, b: Int) : Int =
- if (b == 0) a else gcd(b, a % b)
+def gcd(a: Int, b: Int) : Int = {
+ if (b == 0) a
+ else {
+ val foo = 42
+ gcd(b, a % b)
+ }
+}
gcd(48, 18)
@@ -245,7 +257,7 @@
import scala.util._
import io.Source
-val my_url = "https://nms.kcl.ac.uk/christian.urban/"
+val my_url = "https://nms.imperial.ac.uk/christian.urban/"
Source.fromURL(my_url).mkString
@@ -255,7 +267,7 @@
// the same for files
-Source.fromFile("test.txt").mkString
+Try(Some(Source.fromFile("text.txt").mkString)).getOrElse(None)
// function reading something from files...
@@ -266,7 +278,7 @@
// slightly better - return Nil
def get_contents(name: String) : List[String] =
- Try(Source.fromFile(name).getLines.toList).getOrElse(Nil)
+ Try(Source.fromFile(name).getLines.toList).getOrElse(List())
get_contents("text.txt")
@@ -313,7 +325,10 @@
// For-Comprehensions (not For-Loops)
//====================================
-for (n <- (1 to 10).toList) yield square(n)
+
+for (n <- (1 to 10).toList) yield {
+ square(n) + 1
+}
for (n <- (1 to 10).toList;
m <- (1 to 10).toList) yield m * n
@@ -323,22 +338,24 @@
for (n <- (1 to 10).toList;
m <- (1 to 10).toList) yield m * n
+println(mult_table.mkString)
mult_table.sliding(10,10).mkString("\n")
// the list/set/... can also be constructed in any
// other way
-for (n <- List(10,12,4,5,7,8,10)) yield n * n
+for (n <- Set(10,12,4,5,7,8,10)) yield n * n
// with if-predicates / filters
for (n <- (1 to 3).toList;
m <- (1 to 3).toList;
- if (n + m) % 2 == 0) yield (n, m)
+ if (n + m) % 2 == 0;
+ if (n * m) < 2) yield (n, m)
for (n <- (1 to 3).toList;
m <- (1 to 3).toList;
- if ((n + m) % 2 == 0)) yield (n, m)
+ if ((((n + m) % 2 == 0)))) yield (n, m)
// with patterns
@@ -389,6 +406,8 @@
// BTW: a roundabout way of printing out a list, say
val lst = ('a' to 'm').toList
+for (n <- lst) println(n)
+
for (i <- (0 until lst.length)) println(lst(i))
// Why not just? Why making your life so complicated?