progs/lecture1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 09 Nov 2016 13:36:13 +0000
changeset 24 66b97f9a40f8
parent 23 8ac886bb0c15
child 25 6253f4681451
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
     1
// Lecture 1
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
     2
//==========
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     3
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
     4
//**Assignments (values)**
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
     5
//(variable names should be lower case)
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
     6
//=====================================
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
     7
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     8
val x = 42
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     9
val y = 3 + 4
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    10
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    11
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    12
//**Collections**
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    13
//===============
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    14
List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    15
Set(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    16
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    17
1 to 10
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    18
(1 to 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    19
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    20
(1 until 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    21
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    22
// an element in a list
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    23
List(1, 2, 3, 1)(0)
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    24
List(1, 2, 3, 1)(3)
18
87e55eb309ed updated
Christian Urban <urbanc@in.tum.de>
parents: 15
diff changeset
    25
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    26
1::2::3::Nil
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    27
List(1, 2, 3) ::: List(4, 5, 6)
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    28
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    29
//**Printing/Strings**
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    30
//====================
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    31
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    32
println("test")
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
    33
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
    34
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    35
val tst = "This is a " + "test" 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    36
println(tst)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    37
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    38
val lst = List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    39
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    40
println(lst.toString)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    41
println(lst.mkString("\n"))
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    42
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    43
// some methods take more than one argument
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    44
println(lst.mkString("[", ",", "]"))
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    45
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    46
//**Conversion methods**
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    47
//======================
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    48
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    49
List(1,2,3,1).toString
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    50
List(1,2,3,1).toSet
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    51
"hello".toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    52
1.toDouble
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    53
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    54
//**Types**
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    55
//=========
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    56
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    57
// Int, Long, BigInt, Float, Double
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    58
// String, Char
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    59
// List[Int], Set[Double]
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    60
// Pairs: (Int, String)        
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    61
// List[(BigInt, String)]
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    62
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    63
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    64
//**Smart Strings**
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    65
//=================
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    66
println(">\n<")
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    67
println(""">\n<""")
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    68
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    69
/* in Java
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    70
val lyrics = "Baa, Baa, Black Sheep \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    71
             "Have you any wool? \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    72
             "Yes, sir, yes sir \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    73
             "Three bags full"
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    74
*/ 
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    75
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    76
val lyrics = """Baa, Baa, Black Sheep  
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    77
                |Have you any wool?
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    78
                |Yes, sir, yes sir
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    79
                |Three bags full""".stripMargin
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    80
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    81
println(lyrics)
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    82
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    83
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    84
//**Pairs/Tuples**
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    85
//================
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    86
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    87
val p = (1, "one")
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    88
p._1
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    89
p._2
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    90
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    91
val t = (4,1,2,3)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    92
t._4
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    93
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    94
//**Function Definitions**
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    95
//========================
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    96
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    97
def square(x: Int): Int = x * x
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    98
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    99
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   100
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   101
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   102
//**Ifs control structures**
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   103
//==========================
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   104
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   105
def fact(n: Int): Int = 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   106
  if (n == 0) 1 else n * fact(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   107
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   108
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   109
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   110
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   111
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   112
def fact2(n: BigInt): BigInt = 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   113
  if (n == 0) 1 else n * fact2(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   114
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   115
def fib(n: Int): Int =
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   116
  if (n == 0) 1 else
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   117
    if (n == 1) 1 else fib(n - 1) + f(n - 2)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   118
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   119
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   120
//a recursive function
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   121
def gcd(x: Int, y: Int): Int = 2 //??? 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   122
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   123
//**String Interpolations**
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   124
//=========================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   125
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   126
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   127
//**Assert/Testing**
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   128
====================
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   129
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   130
//**For-Maps (not For-Loops)**
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   131
//============================
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   132
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   133
for (n <- (1 to 10).toList) yield square(n)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   134
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   135
for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   136
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   137
val mtable = for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   138
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   139
mtable.sliding(10,10).toList.mkString(
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   140
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   141
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   142
// webpages