progs/lecture1.scala
author Christian Urban <urbanc@in.tum.de>
Wed, 09 Nov 2016 15:07:23 +0000
changeset 25 6253f4681451
parent 23 8ac886bb0c15
child 26 a7afc2540a88
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
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
     2
//===========
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     3
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
     4
// Assignments (values)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
     5
// (variable names should be lower case)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
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
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    12
// Collections
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    13
//=============
14
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
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    29
// Printing/Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    30
//==================
14
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
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    46
// Conversion methods
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    47
//====================
14
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
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    54
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    55
List(1,2,3,4).reverse
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    56
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    57
// Types
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    58
//=======
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    59
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    60
/* Scala is a strongly typed language
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    61
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    62
 * Base types
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    63
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    64
    Int, Long, BigInt, Float, Double
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    65
    String, Char
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    66
    Boolean
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    67
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    68
 * Compound types 
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    69
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    70
    List[Int],
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    71
    Set[Double]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    72
    Pairs: (Int, String)        
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    73
    List[(BigInt, String)]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    74
*/
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    75
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    76
// Smart Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    77
//===============
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    78
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    79
println(">\n<")
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    80
println(""">\n<""")
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    81
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    82
/* in Java
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    83
val lyrics = "Baa, Baa, Black Sheep \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    84
             "Have you any wool? \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    85
             "Yes, sir, yes sir \n" +
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    86
             "Three bags full"
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    87
*/ 
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    88
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    89
val lyrics = """Baa, Baa, Black Sheep  
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    90
                |Have you any wool?
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    91
                |Yes, sir, yes sir
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    92
                |Three bags full""".stripMargin
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    93
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    94
println(lyrics)
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
    95
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    96
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    97
// Pairs/Tuples
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    98
//==============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    99
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   100
val p = (1, "one")
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   101
p._1
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   102
p._2
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
val t = (4,1,2,3)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   105
t._4
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   106
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   107
// Hello World
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   108
//=============
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   109
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   110
// show an example of a stand-alone scala file
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   111
// remind that in the course work you are asked a 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   112
// plain scala "work-sheet"
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   113
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   114
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   115
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   116
// Function Definitions
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   117
//======================
14
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
def square(x: Int): Int = x * x
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   120
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   121
square(6)
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   122
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   123
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   124
// If control structure
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   125
//======================
14
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
def fact(n: Int): Int = 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   128
  if (n == 0) 1 else n * fact(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   129
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   130
/* boolean operators
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   131
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   132
   ==     equals
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   133
   !      not
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   134
   && ||  and, or
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   135
   
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   136
*/
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   137
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   138
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   139
def fact2(n: BigInt): BigInt = 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   140
  if (n == 0) 1 else n * fact2(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   141
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   142
fact2(150)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   143
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   144
def fib(n: Int): Int =
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   145
  if (n == 0) 1 else
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   146
    if (n == 1) 1 else fib(n - 1) + f(n - 2)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   147
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   148
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   149
//a recursive function
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   150
def gcd(x: Int, y: Int): Int = 2 //??? 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   151
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   152
// String Interpolations
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   153
//=======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   154
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   155
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   156
// Assert/Testing
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   157
//================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   158
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   159
// For-Maps (not For-Loops)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   160
//==========================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   161
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   162
for (n <- (1 to 10).toList) yield square(n)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   163
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   164
for (n <- (1 to 10).toList; 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   165
     m <- (1 to 10).toList) yield m * n
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   166
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   167
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   168
val mtable = for (n <- (1 to 10).toList; 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   169
                  m <- (1 to 10).toList) yield m * n
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   170
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   171
mtable.sliding(10,10).mkString("\n")
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   172
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   173
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   174
// Webpages
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   175
//==========