progs/lecture1.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 07 Nov 2016 12:11:54 +0000
changeset 12 4703aebadd23
parent 8 ab77f6006f1f
child 13 0ce25f816414
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
// toList, toSet, toDouble
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
// function definition
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
// smart strings
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
// maps
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
// recursion
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
// webpages
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
     9
//**Assignment (values)**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    10
//=======================
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    11
val x = 42
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    12
val y = 3 + 4
8
ab77f6006f1f updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
12
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    14
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    15
//**Collections**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    16
//===============
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    17
List(1,2,3,1)
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    18
Set(1,2,3,1)
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    19
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    20
1 to 10
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    21
(1 to 10).toList
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    22
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    23
(1 until 10).toList
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    24
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    25
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    26
//**Printing/Strings**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    27
//====================
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    28
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    29
println("test")
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    30
val tst = "This is a " + "test" 
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    31
println(tst)
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    32
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    33
val lst = List(1,2,3,1)
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    34
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    35
println(lst.toString)
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    36
println(lst.mkString("\n"))
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    37
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    38
// some methods take more than one argument
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    39
println(lst.mkString("[",",","]"))
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    40
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    41
//**Conversion methods**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    42
//======================
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    43
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    44
List(1,2,3,1).toString
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    45
List(1,2,3,1).toSet
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    46
"hello".toList
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    47
1.toDouble
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    48
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    49
//**Types**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    50
//=========
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    51
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    52
// Int, Long, BigInt
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    53
// String, Char
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    54
// List[Int], Set[Double]
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    55
// Pairs: (Int, String)        
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    56
// List[(BigInt, String)]
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    57
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    58
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    59
//**Smart Strings**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    60
//=================
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    61
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    62
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    63
//**Pairs/Tuples**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    64
//================
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    65
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    66
val p = (1, "one")
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    67
p._1
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    68
p._2
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    69
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    70
val t = (4,1,2,3)
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    71
t._4
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    72
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    73
//**Function Definitions**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    74
//========================
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    75
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    76
def square(x: Int): Int = x * x
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    77
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    78
//**Ifs control structures**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    79
//==========================
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    80
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    81
def fact(n: Int): Int = 
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    82
  if (n == 0) 1 else n * fact(n - 1)
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    83
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    84
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    85
def fact2(n: BigInt): BigInt = 
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    86
  if (n == 0) 1 else n * fact2(n - 1)
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    87
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    88
def fib(n: Int): Int =
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    89
  if (n == 0) 1 else
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    90
    if (n == 1) 1 else fib(n - 1) + f(n - 2)
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    91
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    92
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    93
//a recursive function
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    94
def gcd(x: Int, y: Int): Int = 2 //??? 
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    95
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    96
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    97
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    98
//**Assert/Testing**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
    99
====================
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   100
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   101
//**For-Maps (not For-Loops)**
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   102
//============================
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   103
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   104
for (n <- (1 to 10).toList) yield square(n)
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   105
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   106
for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   107
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   108
val mtable = for (n <- (1 to 10).toList; m <- (1 to 10).toList) yield m * n
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   109
4703aebadd23 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   110
mtable.sliding(10,10).toList.mkString(