progs/lecture3.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 23 Feb 2018 22:26:42 +0000
changeset 170 37b1bfcdba79
parent 158 94b11ac19b41
child 178 fdf77ee57cdc
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     1
// Scala Lecture 3
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     2
//=================
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     3
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
     4
// Pattern Matching
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
     5
//==================
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
     6
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
     7
// A powerful tool which is supposed to come to Java in a few years
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
     8
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
     9
// has it for many years. Other functional languages have it already for
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    10
// decades. I think I would be really upset if a programming language 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    11
// I have to use does not have pattern matching....its is just so 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    12
// useful. ;o)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    13
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    14
// The general schema:
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    15
//
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    16
//    expression match {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    17
//       case pattern1 => expression1
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    18
//       case pattern2 => expression2
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    19
//       ...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    20
//       case patternN => expressionN
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    21
//    }
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    22
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    23
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    24
// remember
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    25
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    26
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    27
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    28
def my_flatten(xs: List[Option[Int]]): List[Int] = {
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    29
  if (xs == Nil) Nil
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    30
  else if (xs.head == None) my_flatten(xs.tail)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    31
  else xs.head.get :: my_flatten(xs.tail)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    32
}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    33
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    34
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    35
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    36
val lst = List(None, Some(1), Some(2), None, Some(3))
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    37
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    38
def my_flatten(lst: List[Option[Int]]): List[Int] = lst match {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    39
  case Nil => Nil
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    40
  case None::xs => my_flatten(xs)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    41
  case Some(n)::xs => n::my_flatten(xs)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    42
}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    43
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    44
my_flatten(lst)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    45
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    46
Nil == List()
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    47
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    48
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    49
// another example including a catch-all pattern
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    50
def get_me_a_string(n: Int): String = n match {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    51
  case 0 => "zero"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    52
  case 1 => "one"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    53
  case 2 => "two"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    54
  case _ => "many"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    55
}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    56
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    57
get_me_a_string(10)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    58
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    59
// you can also have cases combined
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    60
def season(month: String) = month match {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    61
  case "March" | "April" | "May" => "It's spring"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    62
  case "June" | "July" | "August" => "It's summer"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    63
  case "September" | "October" | "November" => "It's autumn"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    64
  case "December" | "January" | "February" => "It's winter"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    65
}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    66
 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    67
println(season("November"))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    68
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    69
// What happens if no case matches?
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    70
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    71
println(season("foobar"))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    72
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    73
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    74
// we can also match more complicated pattern
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    75
//
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    76
// let's look at the Collatz function on binary strings
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    77
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    78
// adding two binary strings in a very, very lazy manner
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    79
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    80
def badd(s1: String, s2: String) : String = 
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    81
  (BigInt(s1, 2) + BigInt(s2, 2)).toString(2)
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    82
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    83
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    84
"111".dropRight(1)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    85
"111".last
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    86
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    87
def bcollatz(s: String) : Long = (s.dropRight(1), s.last) match {
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    88
  case ("", '1') => 1                               // we reached 1
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    89
  case (rest, '0') => 1 + bcollatz(rest)            
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    90
                                  // even number => divide by two
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    91
  case (rest, '1') => 1 + bcollatz(badd(s + '1', s))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    92
                                  // odd number => s + '1' is 2 * s + 1
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    93
                                  // add another s gives 3 * s + 1  
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    94
} 
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    95
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    96
bcollatz(6.toBinaryString)
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    97
bcollatz(837799.toBinaryString)
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    98
bcollatz(100000000000000000L.toBinaryString)
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    99
bcollatz(BigInt("1000000000000000000000000000000000000000000000000000000000000000000000000000").toString(2))
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   100
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   101
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   102
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   103
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   104
// User-defined Datatypes
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   105
//========================
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   106
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   107
abstract class Colour
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   108
case object Red extends Colour 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   109
case object Green extends Colour 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   110
case object Blue extends Colour
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   111
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   112
def fav_colour(c: Colour) : Boolean = c match {
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   113
  case Red   => false
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   114
  case Green => true
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   115
  case Blue  => false 
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   116
}
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   117
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   118
fav_colour(Green)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   119
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   120
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   121
// actually colors can be written with "object",
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   122
// because they do not take any arguments
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   123
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   124
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   125
// ... a bit more useful: Roman Numerals
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   126
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   127
abstract class RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   128
case object I extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   129
case object V extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   130
case object X extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   131
case object L extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   132
case object C extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   133
case object D extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   134
case object M extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   135
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   136
type RomanNumeral = List[RomanDigit] 
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   137
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   138
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   139
  case Nil => 0
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   140
  case M::r    => 1000 + RomanNumeral2Int(r)  
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   141
  case C::M::r => 900 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   142
  case D::r    => 500 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   143
  case C::D::r => 400 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   144
  case C::r    => 100 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   145
  case X::C::r => 90 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   146
  case L::r    => 50 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   147
  case X::L::r => 40 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   148
  case X::r    => 10 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   149
  case I::X::r => 9 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   150
  case V::r    => 5 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   151
  case I::V::r => 4 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   152
  case I::r    => 1 + RomanNumeral2Int(r)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   153
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   154
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   155
RomanNumeral2Int(List(I,V))             // 4
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   156
RomanNumeral2Int(List(I,I,I,I))         // 4 (invalid Roman number)
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   157
RomanNumeral2Int(List(V,I))             // 6
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   158
RomanNumeral2Int(List(I,X))             // 9
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   159
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   160
RomanNumeral2Int(List(M,M,X,V,I,I))     // 2017
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   161
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   162
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   163
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   164
// another example
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   165
//=================
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   166
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   167
// Once upon a time, in a complete fictional country there were Persons...
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   168
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   169
abstract class Person
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   170
case object King extends Person
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   171
case class Peer(deg: String, terr: String, succ: Int) extends Person
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   172
case class Knight(name: String) extends Person
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   173
case class Peasant(name: String) extends Person
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   174
case object Clown extends Person
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   175
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   176
def title(p: Person): String = p match {
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   177
  case King => "His Majesty the King"
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   178
  case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   179
  case Knight(name) => s"Sir ${name}"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   180
  case Peasant(name) => name
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   181
  case Clown => "My name is Boris Johnson"
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   182
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   183
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   184
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   185
title(Clown)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   186
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   187
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   188
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   189
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   190
  case (King, _) => true
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   191
  case (Peer(_,_,_), Knight(_)) => true
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   192
  case (Peer(_,_,_), Peasant(_)) => true
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   193
  case (Peer(_,_,_), Clown) => true
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   194
  case (Knight(_), Peasant(_)) => true
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   195
  case (Knight(_), Clown) => true
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   196
  case (Clown, Peasant(_)) => true
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   197
  case _ => false
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   198
}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   199
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   200
val people = List(Knight("David"), 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   201
                  Peer("Duke", "Norfolk", 84), 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   202
                  Peasant("Christian"), 
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   203
                  King, 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   204
                  Clown)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   205
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   206
println(people.sortWith(superior(_, _)).mkString(", "))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   207
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   208
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   209
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   210
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   211
// Tail recursion
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   212
//================
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   213
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   214
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   215
def fact(n: Long): Long = 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   216
  if (n == 0) 1 else n * fact(n - 1)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   217
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   218
fact(10)              //ok
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   219
fact(10000)           // produces a stackoverflow
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   220
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   221
def factT(n: BigInt, acc: BigInt): BigInt =
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   222
  if (n == 0) acc else factT(n - 1, n * acc)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   223
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   224
factT(10, 1)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   225
factT(100000, 1)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   226
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   227
// there is a flag for ensuring a function is tail recursive
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   228
import scala.annotation.tailrec
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   229
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   230
@tailrec
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   231
def factT(n: BigInt, acc: BigInt): BigInt =
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   232
  if (n == 0) acc else factT(n - 1, n * acc)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   233
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   234
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   235
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   236
// for tail-recursive functions the Scala compiler
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   237
// generates loop-like code, which does not need
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   238
// to allocate stack-space in each recursive
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   239
// call; Scala can do this only for tail-recursive
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   240
// functions
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   241
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   242
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   243
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   244
// sudoku again
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   245
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   246
val game0 = """.14.6.3..
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   247
              |62...4..9
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   248
              |.8..5.6..
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   249
              |.6.2....3
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   250
              |.7..1..5.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   251
              |5....9.6.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   252
              |..6.2..3.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   253
              |1..5...92
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   254
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   255
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   256
type Pos = (Int, Int)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   257
val EmptyValue = '.'
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   258
val MaxValue = 9
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   259
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   260
val allValues = "123456789".toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   261
val indexes = (0 to 8).toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   262
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   263
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   264
def empty(game: String) = game.indexOf(EmptyValue)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   265
def isDone(game: String) = empty(game) == -1 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   266
def emptyPosition(game: String) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   267
  (empty(game) % MaxValue, empty(game) / MaxValue)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   268
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   269
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   270
def get_row(game: String, y: Int) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   271
  indexes.map(col => game(y * MaxValue + col))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   272
def get_col(game: String, x: Int) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   273
  indexes.map(row => game(x + row * MaxValue))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   274
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   275
def get_box(game: String, pos: Pos): List[Char] = {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   276
    def base(p: Int): Int = (p / 3) * 3
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   277
    val x0 = base(pos._1)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   278
    val y0 = base(pos._2)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   279
    val ys = (y0 until y0 + 3).toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   280
    (x0 until x0 + 3).toList.flatMap(x => ys.map(y => game(x + y * MaxValue)))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   281
}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   282
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   283
// this is not mutable!!
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   284
def update(game: String, pos: Int, value: Char): String = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   285
  game.updated(pos, value)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   286
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   287
def toAvoid(game: String, pos: Pos): List[Char] = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   288
  (get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   289
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   290
def candidates(game: String, pos: Pos): List[Char] = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   291
  allValues.diff(toAvoid(game,pos))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   292
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   293
//candidates(game0, (0,0))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   294
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   295
def pretty(game: String): String = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   296
  "\n" + (game sliding (MaxValue, MaxValue) mkString "\n")
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   297
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   298
/////////////////////
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   299
// not tail recursive 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   300
def search(game: String): List[String] = {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   301
  if (isDone(game)) List(game)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   302
  else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   303
    val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   304
    cs.map(c => search(update(game, empty(game), c))).toList.flatten
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   305
  }
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   306
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   307
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   308
// tail recursive version that searches 
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   309
// for all solutions
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   310
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   311
def searchT(games: List[String], sols: List[String]): List[String] = games match {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   312
  case Nil => sols
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   313
  case game::rest => {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   314
    if (isDone(game)) searchT(rest, game::sols)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   315
    else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   316
      val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   317
      searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   318
    }
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   319
  }
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   320
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   321
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   322
searchT(List(game3), List()).map(pretty)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   323
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   324
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   325
// tail recursive version that searches 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   326
// for a single solution
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   327
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   328
def search1T(games: List[String]): Option[String] = games match {
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   329
  case Nil => None
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   330
  case game::rest => {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   331
    if (isDone(game)) Some(game)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   332
    else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   333
      val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   334
      search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   335
    }
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   336
  }
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   337
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   338
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   339
search1T(List(game3)).map(pretty)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   340
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   341
// game with multiple solutions
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   342
val game3 = """.8...9743
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   343
              |.5...8.1.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   344
              |.1.......
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   345
              |8....5...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   346
              |...8.4...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   347
              |...3....6
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   348
              |.......7.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   349
              |.3.5...8.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   350
              |9724...5.""".stripMargin.replaceAll("\\n", "")
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   351
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   352
searchT(List(game3), Nil).map(pretty)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   353
search1T(List(game3)).map(pretty)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   354
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   355
// Moral: Whenever a recursive function is resource-critical
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   356
// (i.e. works with large recursion depth), then you need to
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   357
// write it in tail-recursive fashion.
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   358
// 
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   359
// Unfortuantely, Scala because of current limitations in 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   360
// the JVM is not as clever as other functional languages. It can 
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   361
// only optimise "self-tail calls". This excludes the cases of 
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   362
// multiple functions making tail calls to each other. Well,
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   363
// nothing is perfect. 
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   364
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   365
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   366
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   367
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   368
// Polymorphic Types
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   369
//===================
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   370
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   371
// You do not want to write functions like contains, first 
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   372
// and so on for every type of lists.
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   373
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   374
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   375
def length_string_list(lst: List[String]): Int = lst match {
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   376
  case Nil => 0
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   377
  case x::xs => 1 + length_string_list(xs)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   378
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   379
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   380
def length_int_list(lst: List[Int]): Int = lst match {
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   381
  case Nil => 0
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   382
  case x::xs => 1 + length_int_list(xs)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   383
}
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   384
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   385
length_string_list(List("1", "2", "3", "4"))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   386
length_int_list(List(1, 2, 3, 4))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   387
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   388
//-----
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   389
def length[A](lst: List[A]): Int = lst match {
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   390
  case Nil => 0
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   391
  case x::xs => 1 + length(xs)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   392
}
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   393
length(List("1", "2", "3", "4"))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   394
length(List(King, Knight("foo"), Clown))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   395
length(List(1, 2, 3, 4))
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   396
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   397
def map[A, B](lst: List[A], f: A => B): List[B] = lst match {
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   398
  case Nil => Nil
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   399
  case x::xs => f(x)::map_int_list(xs, f) 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   400
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   401
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   402
map_int_list(List(1, 2, 3, 4), square)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   403
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   404
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   405
// Remember?
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   406
def first[A, B](xs: List[A], f: A => Option[B]): Option[B] = ...
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   407
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   408
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   409
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   410
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   411
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   412
// Cool Stuff
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   413
//============
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   414
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   415
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   416
// Implicits 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   417
//===========
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   418
//
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   419
// For example adding your own methods to Strings:
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   420
// Imagine you want to increment strings, like
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   421
//
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   422
//     "HAL".increment
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   423
//
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   424
// you can avoid ugly fudges, like a MyString, by
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   425
// using implicit conversions.
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   426
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   427
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   428
implicit class MyString(s: String) {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   429
  def increment = for (c <- s) yield (c + 1).toChar 
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   430
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   431
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   432
"HAL".increment
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   433
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   434
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   435
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   436
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   437
// Regular expressions - the power of DSLs in Scala
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   438
//==================================================
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   439
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   440
abstract class Rexp
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   441
case object ZERO extends Rexp                       // nothing
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   442
case object ONE extends Rexp                        // the empty string
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   443
case class CHAR(c: Char) extends Rexp               // a character c
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   444
case class ALT(r1: Rexp, r2: Rexp) extends Rexp     // alternative  r1 + r2
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   445
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp     // sequence     r1 o r2  
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   446
case class STAR(r: Rexp) extends Rexp               // star         r*
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   447
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   448
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   449
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   450
// (ab)*
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   451
val r0 = STAR(SEQ(CHAR('a'), CHAR('b')))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   452
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   453
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   454
// some convenience for typing in regular expressions
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   455
import scala.language.implicitConversions    
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   456
import scala.language.reflectiveCalls 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   457
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   458
def charlist2rexp(s: List[Char]): Rexp = s match {
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   459
  case Nil => ONE
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   460
  case c::Nil => CHAR(c)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   461
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   462
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   463
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   464
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   465
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   466
val r1 = STAR("ab")
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   467
val r2 = STAR(ALT("ab"))
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   468
val r3 = STAR(ALT("ab", "baa baa black sheep"))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   469
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   470
implicit def RexpOps (r: Rexp) = new {
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   471
  def | (s: Rexp) = ALT(r, s)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   472
  def % = STAR(r)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   473
  def ~ (s: Rexp) = SEQ(r, s)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   474
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   475
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   476
implicit def stringOps (s: String) = new {
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   477
  def | (r: Rexp) = ALT(s, r)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   478
  def | (r: String) = ALT(s, r)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   479
  def % = STAR(s)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   480
  def ~ (r: Rexp) = SEQ(s, r)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   481
  def ~ (r: String) = SEQ(s, r)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   482
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   483
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   484
//example regular expressions
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   485
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   486
val sign = "+" | "-" | ""
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   487
val number = sign ~ digit ~ digit.% 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   488
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   489
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   490
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   491
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   492
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   493
// The End
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   494
//=========
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   495
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   496
// A function should do one thing, and only one thing.
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   497
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   498
// Make your variables immutable, unless there's a good 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   499
// reason not to.
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   500
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   501
// You can be productive on Day 1, but the language is deep.
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   502
//
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   503
// http://scalapuzzlers.com
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   504
//
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   505
// http://www.latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   506
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   507
List(1, 2, 3) contains "your mom"
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   508
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   509
// I like best about Scala that it lets me often write
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   510
// concise, readable code.
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 67
diff changeset
   511
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   512
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   513
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   514
// You can define your own while loop
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   515
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   516
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   517
def my_while(condition: => Boolean)(block: => Unit): Unit = 
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   518
  if (condition) { block ; my_while(condition) { block } } else { }
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   519
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   520
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   521
var x = 10
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   522
my_while (x > 0) { 
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   523
  println(s"$x") ; x = x - 1 
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   524
}
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   525
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   526
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   527
`symbol
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   528
`symbol`