progs/lecture1.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 08 Dec 2023 00:54:36 +0000
changeset 481 e03a0100ec46
parent 471 135bf034ac30
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 36
diff changeset
     1
// Scala Lecture 1
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 36
diff changeset
     2
//=================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
     3
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
     4
// - List, Sets, Ints, Strings, ... 
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
     5
// - Value assignments (val vs var)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
     6
// - How to define functions? (What is returned?)
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
     7
// - If-Conditions
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
     8
// - For-Comprehensions (guards, with/without yield)
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
     9
// - String-Interpolations
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    10
//
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
    11
//
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
    12
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    13
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
    14
// Value assignments
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    15
// (their names should be lower case)
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    16
//====================================
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
    17
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    18
353
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    19
val x = 42
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    20
val y = 3 + 4 
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    21
val z = x / y
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
    22
val x = 0
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
    23
println(z)
353
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    24
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
    25
353
bb6074814a73 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 335
diff changeset
    26
// (you cannot reassign values: z = 9 will give an error)
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
    27
var z = 9
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
    28
z = 10
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    29
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    30
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    31
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents: 124
diff changeset
    32
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    33
// Collections
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    34
//=============
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
    35
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    36
List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    37
Set(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    38
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    39
// picking an element in a list
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    40
val lst = List(1, 2, 3, 1)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    41
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
    42
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    43
lst(0)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    44
lst(2)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    45
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    46
// head and tail
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    47
lst.head
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    48
lst.tail
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    49
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    50
// some alterative syntax for lists
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    51
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    52
Nil     // empty list
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    53
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    54
1 :: 2 :: 3 :: Nil
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    55
List(1, 2, 3) ::: List(4, 5, 6)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    56
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    57
// also
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    58
List(1, 2, 3) ++ List(3, 6, 5)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    59
Set(1, 2, 3) ++ Set(3, 6, 5)
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
    60
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
    61
// ranges
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    62
1 to 10
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    63
(1 to 10).toList
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    64
(1 to 10).toList.toString
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    65
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    66
(1 until 10).toList
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    67
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
    68
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
    69
// Equality in Scala is structural
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
    70
//=================================
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
    71
val a = "Dave2"
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    72
val b = "Dave"
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    73
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
    74
if (a == b) println("Equal") else println("Unequal")
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    75
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    76
Set(1,2,3) == Set(3,1,2)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    77
List(1,2,3) == List(3,1,2)
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    78
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    79
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    80
// this applies to "concrete" values...pretty much 
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    81
// everything; but for example you cannot compare 
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
    82
// functions (later), and also not arrays
313
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    83
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    84
Array(1) == Array(1)
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    85
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
    86
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    87
// Printing/Strings
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
    88
//==================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    89
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    90
println("test")
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
    91
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
    92
val tst = "This is a " ++ "test" 
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
    93
313
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    94
print(tst) 
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
    95
println(tst) 
14
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
val lst = List(1,2,3,1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    98
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
    99
println(lst.toString)
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   100
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   101
println(lst.mkString)
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   102
println(lst.mkString(","))
14
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
// some methods take more than one argument
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   105
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   106
println(lst.mkString("{", ",", "}"))
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   107
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   108
// (in this case .mkString can take no, one, 
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   109
// or three arguments...this has to do with
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   110
// default arguments)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   111
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   112
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   113
// Conversion methods
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   114
//====================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   115
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   116
List(1,2,3,1).toString
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   117
List(1,2,3,1).toSet
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   118
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   119
"hello".toList
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   120
"hello".toSet
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   121
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   122
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   123
1.toDouble
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   124
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   125
1   // an Int
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   126
1L  // a Long
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   127
1F  // a Float
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   128
1D  // a Double
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   129
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   130
// useful methods for lists
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   131
//=========================
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   132
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   133
List(1,2,3,4).length
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   134
List(1,2,3,4).reverse
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   135
List(1,2,3,4).max
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   136
List(1,2,3,4).min
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   137
List(1,2,3,4).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   138
List(1,2,3,4).take(2).sum
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   139
List(1,2,3,4).drop(2).sum
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   140
List(1,2,3,4,3).indexOf(3)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   141
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   142
"1,2,3,4,5".split(",").mkString("\n")
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   143
"1,2,3,4,5".split(",").toList
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   144
"1,2,3,4,5".split(",3,").mkString("\n")
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   145
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   146
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   147
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   148
// Types (see slide)
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   149
//===================
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   150
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   151
/* Scala is a strongly typed language
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   152
 
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   153
 * base types
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   154
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   155
    Int, Long, BigInt, Float, Double
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   156
    String, Char
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   157
    Boolean...
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   158
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   159
 * compound types 
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   160
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   161
    List[Int]
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   162
    Set[Double]
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   163
    Pairs: (Int, String)        
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   164
    List[(BigInt, String)]
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   165
    Option[Int]
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   166
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   167
 * user-defined types (later)
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   168
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   169
*/
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   170
23
8ac886bb0c15 updated
Christian Urban <urbanc@in.tum.de>
parents: 21
diff changeset
   171
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   172
// you can make the type of a value explicit
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   173
val name = "bob"
247
50a3b874008a updated
Christian Urban <urbanc@in.tum.de>
parents: 202
diff changeset
   174
447
f51e593903ac updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 444
diff changeset
   175
val name : String = "bob"
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   176
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   177
// type errors
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   178
math.sqrt("64")
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   179
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   180
// produces
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   181
//
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   182
// Type Mismatch Error:
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   183
//   Found   : ("64" : String)
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   184
//   Required: Double
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   185
//
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   186
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   187
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   188
// Pairs/Tuples
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   189
//==============
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   190
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   191
val p = (1, "one")
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   192
p._1
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   193
p._2
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   194
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   195
val t = (4,1,2,3)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   196
t._4
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   197
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   198
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   199
List(("one", 1), ("two", 2), ("three", 3))
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   200
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   201
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   202
// Function Definitions
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   203
//======================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   204
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   205
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   206
def incr(x: Int) : Int = x + 1
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   207
def double(x: Int) : Int = x + x
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   208
def square(x: Int) : Int = x * x
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   209
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   210
def str(x: Int) : String = x.toString
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   211
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   212
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   213
incr(3)
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   214
double(4)
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   215
square(6)
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   216
str(3)
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   217
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   218
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   219
// The general scheme for a function: you have to give a 
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   220
// type to each argument and a return type of the function
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   221
//
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   222
//  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   223
//     ....
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   224
//  }
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   225
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   226
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   227
// > LENGTH OF LIST EXAMPLE
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   228
def len(xs: List[Int], acc: Int) : Int = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   229
   if (xs == Nil) acc
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   230
   else foo(xs.tail, acc + 1)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   231
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   232
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   233
def len(xs: List[Int]) : Int = foo(xs, 0)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   234
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   235
len(List(1,2,3,4,1))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   236
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   237
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   238
def len(xs: List[Int]) : Int = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   239
    if (xs == Nil) 0
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   240
    else (1 + len(xs.tail))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   241
}    
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   242
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   243
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   244
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   245
len(List(1,2,3,4,1))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   246
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   247
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   248
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   249
def len(xs: List[Int]) : Int = xs match {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   250
   case Nil => 0
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   251
   case x :: xs => 1 + len(xs)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   252
}
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   253
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   254
len(List(1,2,3,4,1))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   255
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   256
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   257
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   258
// If-Conditionals
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   259
//=================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   260
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   261
// - Scala used to not have a then-keyword
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   262
// - !!both if-else branches need to be present!!
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   263
143
11396c17cd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 140
diff changeset
   264
def fact(n: Int) : Int = 
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   265
  if (n == 0) 1 else n * fact(n - 1)
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   266
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   267
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   268
// Scala 3 introduced if-then-else - maybe people 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   269
// desperately needed it 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   270
def fact(n: Int) : Int = 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   271
  if n == 0 then 1 else n * fact(n - 1)
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   272
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   273
fact(5)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   274
fact(150)
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   275
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   276
/* boolean operators
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   277
 
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   278
   ==     equals
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   279
   !=     not equals
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   280
   !      not
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   281
   && ||  and, or
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   282
*/
15
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   283
52713e632ac0 updated
Christian Urban <urbanc@in.tum.de>
parents: 14
diff changeset
   284
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   285
def fib(n: Int) : Int = {
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   286
  if (n == 0) 1 else
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   287
    if (n == 1) 1 else fib(n - 1) + fib(n - 2)
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   288
}
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   289
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   290
fib(9)
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   291
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   292
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   293
//gcd - Euclid's algorithm
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   294
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   295
def gcd(a: Int, b: Int) : Int = {
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   296
  if (b == 0) a 
272
da3d30ae67ec updated
Christian Urban <urbanc@in.tum.de>
parents: 268
diff changeset
   297
  else  gcd(b, a % b)
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   298
}
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   299
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   300
gcd(48, 18)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   301
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   302
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   303
def power(x: Int, n: Int) : Int =
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   304
  if (n == 0) 1 else x * power(x, n - 1) 
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   305
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   306
power(5, 5)
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   307
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   308
// BTW: no returns!!
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   309
// "last" line (expression) in a function determines the 
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 353
diff changeset
   310
// result
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   311
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   312
def average(xs: List[Int]) : Int = {
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   313
  if (xs.length == 0) 0 
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   314
  else xs.sum / xs.length
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   315
}
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   316
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   317
average(List(3,4,5))
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   318
average(List())
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   319
average(Nil)
359
8aaf187d25f0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
   320
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   321
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   322
// For-Comprehensions (not For-Loops)
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   323
//====================================
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   324
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   325
val lst = (1 to 10).toList
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   326
for (n <- lst) yield n * n 
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   327
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   328
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   329
for (n <- lst) yield { 
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   330
  square(n) + double(n)
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   331
}
14
2d9defa8ad7f updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   332
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   333
for (n <- (1 to 10).toList; 
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   334
     m <- (1 to 5).toList) yield (n, m)
21
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   335
Christian Urban <urbanc@in.tum.de>
parents: 18
diff changeset
   336
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   337
// you can assign the result of a for-comprehension
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   338
// to a value
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   339
val mult_table = 
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   340
  for (n <- (1 to 10).toList; 
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   341
       m <- (1 to 10).toList) yield n * m
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   342
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   343
println(mult_table.mkString(","))
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   344
mult_table.sliding(10,10).toList
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   345
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   346
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   347
.mkString("\n")
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   348
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   349
// for-comprehensions also work for other
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   350
// collections
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   351
202
f7bcb27d1940 updated
Christian Urban <urbanc@in.tum.de>
parents: 200
diff changeset
   352
for (n <- Set(10,12,4,5,7,8,10)) yield n * n
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   353
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   354
for (n <- (1 to 10)) yield {
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   355
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   356
  n * n  
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   357
}
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   358
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   359
// with if-predicates / filters
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   360
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   361
val xs = for (n <- (1 to 3).toList; 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   362
     m <- (1 to 3).toList) yield (n,m)
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   363
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   364
xs.filter{case (m, n) => (n + m) % 2 == 0}    
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   365
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   366
for (n <- (1 to 3).toList; 
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   367
     m <- (1 to 3).toList;
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   368
     if (n + m) % 2 == 0) yield (n, m)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   369
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   370
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   371
// with patterns
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   372
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   373
val lst = List((1, 4), (2, 3), (3, 2), (4, 1))
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   374
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   375
` yield m + n 
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   376
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   377
for (p <- lst) yield p._1 + p._2 
26
a7afc2540a88 updated
Christian Urban <urbanc@in.tum.de>
parents: 25
diff changeset
   378
25
6253f4681451 updated
Christian Urban <urbanc@in.tum.de>
parents: 23
diff changeset
   379
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   380
// general pattern of for-yield 
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   381
// (yield can be several lines)
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   382
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   383
for (pat <- ...) yield {
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   384
  // potentially complicated
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   385
  // calculation of a result
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   386
}
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   387
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   388
// For without yield
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   389
//===================
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   390
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   391
// with only a side-effect (no list is produced),
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   392
// has no "yield"
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   393
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   394
val xs = for (n <- (1 to 10).toList) yield println(n * n)
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   395
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   396
xs.tail
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   397
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   398
val foo = for (n <- (1 to 10).toList) yield n * n
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   399
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   400
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   401
// BTW: a roundabout way of printing out a list, say
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   402
val lst = ('a' to 'm').toList
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   403
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   404
for (i <- (0 until lst.length)) println(lst(i))
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   405
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   406
// ...why not just the following? Why making your life 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   407
// so complicated?
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   408
for (c <- lst) println(c)
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   409
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   410
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 359
diff changeset
   411
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   412
// Functions producing multiple outputs
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   413
//======================================
189
ff815ca0bbcf updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   414
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   415
def get_ascii(c: Char) : (Char, Int) = 
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   416
  (c, c.toInt)
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   417
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   418
get_ascii('a')
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   419
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   420
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   421
// .maxBy, sortBy with pairs
314
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   422
def get_length(s: String) : (String, Int) = 
21b52310bd8b updated
Christian Urban <urbanc@in.tum.de>
parents: 313
diff changeset
   423
  (s, s.length) 
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   424
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   425
val lst = List("zero", "one", "two", "three", "four", "ten")
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   426
val strs = for (s <- lst) yield get_length(s)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   427
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   428
strs.sortBy(_._2)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   429
strs.sortBy(_._1)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   430
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   431
strs.maxBy(_._2)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   432
strs.maxBy(_._1)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   433
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   434
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   435
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   436
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   437
199
54befaf23648 updated
Christian Urban <urbanc@in.tum.de>
parents: 195
diff changeset
   438
// Aside: concurrency 
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   439
// scala-cli --extra-jars scala-parallel-collections_3-1.0.4.jar 
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   440
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   441
for (n <- (1 to 10)) println(n)
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   442
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   443
import scala.collection.parallel.CollectionConverters._
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   444
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   445
for (n <- (1 to 10).par) println(n)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   446
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   447
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 34
diff changeset
   448
// for measuring time
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   449
def time_needed[T](n: Int, code: => T) = {
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   450
  val start = System.nanoTime()
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   451
  for (i <- (0 to n)) code
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   452
  val end = System.nanoTime()
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   453
  (end - start) / 1.0e9
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   454
}
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   455
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   456
val list = (1L to 10_000_000L).toList
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   457
time_needed(10, for (n <- list) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   458
time_needed(10, for (n <- list.par) yield n + 42)
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   459
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   460
// ...but par does not make everything faster
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   461
273
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   462
list.sum
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   463
list.par.sum
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   464
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   465
time_needed(10, list.sum)
b19227660752 updated
Christian Urban <urbanc@in.tum.de>
parents: 272
diff changeset
   466
time_needed(10, list.par.sum)
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   467
140
ecec79b9ab25 updated
Christian Urban <urbanc@in.tum.de>
parents: 137
diff changeset
   468
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   469
// Mutable vs Immutable
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   470
//======================
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   471
//
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   472
// Remember:
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   473
// - no vars, no ++i, no +=
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   474
// - no mutable data-structures (no Arrays, no ListBuffers)
137
53c769a78a1e updated
Christian Urban <urbanc@in.tum.de>
parents: 125
diff changeset
   475
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   476
// But what the heck....lets try to count to 1 Mio in parallel
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   477
// 
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   478
// requires
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   479
// scala-cli --extra-jars scala- parallel-collections_3-1.0.4.jar
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   480
444
7a0735db4788 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 367
diff changeset
   481
import scala.collection.parallel.CollectionConverters._
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   482
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   483
def test() = {
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   484
  var cnt = 0
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   485
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   486
  for(i <- (1 to 100_000)) cnt += 1
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   487
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   488
  println(s"Should be 100000: $cnt")
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   489
}
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   490
481
e03a0100ec46 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 471
diff changeset
   491
test()
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   492
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 316
diff changeset
   493
// Or
313
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   494
// Q: Count how many elements are in the intersections of 
dea46bdfd648 updated
Christian Urban <urbanc@in.tum.de>
parents: 310
diff changeset
   495
//    two sets?
268
e43f7e92ba26 updated
Christian Urban <urbanc@in.tum.de>
parents: 265
diff changeset
   496
// A; IMPROPER WAY (mutable counter)
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   497
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   498
def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   499
  var count = 0
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   500
  for (x <- A.par; if B contains x) count += 1 
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   501
  count
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   502
}
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   503
308
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   504
val A = (0 to 999).toSet
e86add5a6961 updated
Christian Urban <urbanc@in.tum.de>
parents: 273
diff changeset
   505
val B = (0 to 999 by 4).toSet
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   506
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   507
count_intersection(A, B)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   508
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   509
// but do not try to add .par to the for-loop above
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   510
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   511
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   512
//propper parallel version
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   513
def count_intersection2(A: Set[Int], B: Set[Int]) : Int = 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   514
  A.par.count(x => B contains x)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   515
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   516
count_intersection2(A, B)
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   517
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   518
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   519
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   520
// String Interpolations
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   521
//=======================
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   522
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   523
def cube(n: Int) : Int = n * n * n
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   524
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   525
val n = 3
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   526
println("The cube of " + n + " is " + cube(n) + ".")
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   527
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   528
println(s"The cube of $n is ${cube(n)}.")
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   529
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   530
// or even
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   531
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   532
println(s"The cube of $n is ${n * n * n}.")
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   533
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   534
// helpful for debugging purposes
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   535
//
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   536
//     "The most effective debugging tool is still careful 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   537
//          thought, coupled with judiciously placed print 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   538
//                                             statements."
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   539
//       — Brian W. Kernighan, in Unix for Beginners (1979)
265
59779ce322a6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   540
310
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   541
335079d938aa updated
Christian Urban <urbanc@in.tum.de>
parents: 308
diff changeset
   542
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   543
def gcd_db(a: Int, b: Int) : Int = {
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   544
  println(s"Function called with $a and $b.")
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   545
  if (b == 0) a else gcd_db(b, a % b)
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   546
}
367
e6ae724255a8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   547
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   548
gcd_db(48, 18)
367
e6ae724255a8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   549
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   550
// you can also implement your own string interpolations
367
e6ae724255a8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   551
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   552
extension (sc: StringContext) {
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   553
    def i(args: Any*): String = s"\t${sc.s(args:_*)}\n"
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   554
    def l(args: Any*): String = s"${sc.s(args:_*)}:\n"
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   555
}
367
e6ae724255a8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   556
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   557
// this allows you to write things like
367
e6ae724255a8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 364
diff changeset
   558
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   559
i"add ${3+2}" 
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   560
l"some_fresh_name"
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   561
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   562
// Further Information
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   563
//=====================
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   564
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   565
// We are going to use Scala 3 and the scala-cli repl (easier to use)
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   566
//
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   567
//  https://scala-cli.virtuslab.org
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   568
//
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   569
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   570
// The Scala homepage and general information is at
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   571
//
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   572
//  http://www.scala-lang.org
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   573
//	http://docs.scala-lang.org
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   574
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   575
//
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   576
// It should be fairly easy to install the scala-cli binary and
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   577
// run Scala on the commandline. People also use Scala with 
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   578
// Vim and Jedit. I currently settled on Codium
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   579
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   580
//   https://code.visualstudio.com
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   581
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   582
// There are also plugins for Eclipse and IntelliJ - YMMV.
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   583
// Finally there are online editors specifically designed for 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   584
// running Scala applications (but do not blame me if you lose 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   585
// all what you typed in):
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   586
//
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   587
//   https://scalafiddle.io 
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   588
//   https://scastie.scala-lang.org
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   589
//
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   590
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   591
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   592
// Scala Library Docs
124
c45d3cd9a749 updated
Christian Urban <urbanc@in.tum.de>
parents: 123
diff changeset
   593
//====================
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   594
//
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   595
//  https://dotty.epfl.ch/api/index.html
123
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   596
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   597
// Scala Tutorials
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   598
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   599
//  http://docs.scala-lang.org/tutorials/
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   600
//
556cd74cbba9 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   601
// There are also a massive number of Scala tutorials on youtube
200
01ee4b576eb2 updated
Christian Urban <urbanc@in.tum.de>
parents: 199
diff changeset
   602
// and there are tons of books and free material. Google is your 
471
135bf034ac30 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 447
diff changeset
   603
// friend. Just make sure you follow newer material about Scala 3.
32
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   604
45557ad18ea6 updated
Christian Urban <urbanc@in.tum.de>
parents: 26
diff changeset
   605