progs/lecture1.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Thu, 10 Nov 2016 01:31:12 +0000
changeset 31 d0caa12ab8d8
parent 26 a7afc2540a88
child 32 45557ad18ea6
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
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
     4
// Value assignments
25
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
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
    35
val tst = "This is a " ++ "test\n" 
14
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
*/
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   136
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   137
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   138
def fact2(n: BigInt): BigInt = 
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   139
  if (n == 0) 1 else n * fact2(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   140
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   141
fact2(150)
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   142
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
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
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   146
    if (n == 1) 1 else fib(n - 1) + fib(n - 2)
14
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
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   149
//gcd - Euclid's algorithm
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   150
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   151
def gcd(a: Int, b: Int): Int =
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   152
  if (b == 0) a else gcd(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   153
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   154
gcd(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   155
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   156
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   157
// String Interpolations
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   158
//=======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   159
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   160
val n = 3
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   161
println("The square of " + n + " is " + square(n) + ".")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   162
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   163
println(s"The square of ${n} is ${square(n)}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   164
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   165
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   166
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   167
def gcd_db(a: Int, b: Int): Int = {
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   168
  println(s"Function called with ${a} and ${b}.")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   169
  if (b == 0) a else gcd_db(b, a % b)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   170
}
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   171
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   172
gcd_db(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   173
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   174
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   175
// Assert/Testing
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   176
//================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   177
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   178
// For-Comprehensions (not For-Loops)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   179
//====================================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   180
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   181
for (n <- (1 to 10).toList) yield square(n)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   182
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   183
for (n <- (1 to 10).toList; 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   184
     m <- (1 to 10).toList) yield m * n
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   185
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   186
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   187
val mult_table = 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   188
  for (n <- (1 to 10).toList; 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   189
       m <- (1 to 10).toList) yield m * n
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   190
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   191
mult_table.sliding(10,10).mkString("\n")
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   192
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   193
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   194
// with patterns
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   195
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   196
for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   197
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   198
for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   199
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   200
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   201
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   202
// Webpages
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   203
//==========