progs/lecture3.scala
author Christian Urban <urbanc@in.tum.de>
Mon, 11 Jun 2018 14:48:18 +0100
changeset 178 fdf77ee57cdc
parent 170 37b1bfcdba79
child 194 060b081523de
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
178
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   124
abstract class Day
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   125
case object Monday extends Day 
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   126
case object Tuesday extends Day 
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   127
case object Wednesday extends Day
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   128
case object Thursday extends Day 
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   129
case object Friday extends Day 
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   130
case object Saturday extends Day
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   131
case object Sunday extends Day 
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   132
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   133
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   134
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   135
// ... a bit more useful: Roman Numerals
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   136
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   137
abstract class RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   138
case object I extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   139
case object V extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   140
case object X extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   141
case object L extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   142
case object C extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   143
case object D extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   144
case object M extends RomanDigit 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   145
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   146
type RomanNumeral = List[RomanDigit] 
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   147
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   148
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   149
  case Nil => 0
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   150
  case M::r    => 1000 + RomanNumeral2Int(r)  
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   151
  case C::M::r => 900 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   152
  case D::r    => 500 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   153
  case C::D::r => 400 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   154
  case C::r    => 100 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   155
  case X::C::r => 90 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   156
  case L::r    => 50 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   157
  case X::L::r => 40 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   158
  case X::r    => 10 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   159
  case I::X::r => 9 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   160
  case V::r    => 5 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   161
  case I::V::r => 4 + RomanNumeral2Int(r)
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   162
  case I::r    => 1 + RomanNumeral2Int(r)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   163
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   164
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   165
RomanNumeral2Int(List(I,V))             // 4
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   166
RomanNumeral2Int(List(I,I,I,I))         // 4 (invalid Roman number)
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   167
RomanNumeral2Int(List(V,I))             // 6
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   168
RomanNumeral2Int(List(I,X))             // 9
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   169
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   170
RomanNumeral2Int(List(M,M,X,V,I,I))     // 2017
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   171
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   172
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   173
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   174
// another example
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
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   177
// 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
   178
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   179
abstract class Person
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   180
case object King extends Person
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   181
case class Peer(deg: String, terr: String, succ: Int) extends Person
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   182
case class Knight(name: String) extends Person
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   183
case class Peasant(name: String) extends Person
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   184
case object Clown extends Person
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   185
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   186
def title(p: Person): String = p match {
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   187
  case King => "His Majesty the King"
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   188
  case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   189
  case Knight(name) => s"Sir ${name}"
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   190
  case Peasant(name) => name
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   191
  case Clown => "My name is Boris Johnson"
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   192
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   193
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   194
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   195
title(Clown)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   196
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   197
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   198
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   199
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   200
  case (King, _) => true
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   201
  case (Peer(_,_,_), Knight(_)) => true
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   202
  case (Peer(_,_,_), Peasant(_)) => true
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   203
  case (Peer(_,_,_), Clown) => true
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   204
  case (Knight(_), Peasant(_)) => true
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   205
  case (Knight(_), Clown) => true
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   206
  case (Clown, Peasant(_)) => true
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   207
  case _ => false
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   208
}
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
val people = List(Knight("David"), 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   211
                  Peer("Duke", "Norfolk", 84), 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   212
                  Peasant("Christian"), 
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   213
                  King, 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   214
                  Clown)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   215
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   216
println(people.sortWith(superior(_, _)).mkString(", "))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   217
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   218
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   219
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
// Tail recursion
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   222
//================
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   223
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   224
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   225
def fact(n: Long): Long = 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   226
  if (n == 0) 1 else n * fact(n - 1)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   227
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   228
fact(10)              //ok
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   229
fact(10000)           // produces a stackoverflow
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   230
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   231
def factT(n: BigInt, acc: BigInt): BigInt =
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   232
  if (n == 0) acc else factT(n - 1, n * acc)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   233
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   234
factT(10, 1)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   235
factT(100000, 1)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   236
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   237
// there is a flag for ensuring a function is tail recursive
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   238
import scala.annotation.tailrec
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   239
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   240
@tailrec
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   241
def factT(n: BigInt, acc: BigInt): BigInt =
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   242
  if (n == 0) acc else factT(n - 1, n * acc)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   243
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   244
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   245
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   246
// for tail-recursive functions the Scala compiler
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   247
// generates loop-like code, which does not need
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   248
// to allocate stack-space in each recursive
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   249
// call; Scala can do this only for tail-recursive
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   250
// functions
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   251
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   252
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   253
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   254
// sudoku again
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   255
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   256
val game0 = """.14.6.3..
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   257
              |62...4..9
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   258
              |.8..5.6..
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   259
              |.6.2....3
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   260
              |.7..1..5.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   261
              |5....9.6.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   262
              |..6.2..3.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   263
              |1..5...92
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   264
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   265
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   266
type Pos = (Int, Int)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   267
val EmptyValue = '.'
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   268
val MaxValue = 9
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   269
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   270
val allValues = "123456789".toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   271
val indexes = (0 to 8).toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   272
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   273
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   274
def empty(game: String) = game.indexOf(EmptyValue)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   275
def isDone(game: String) = empty(game) == -1 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   276
def emptyPosition(game: String) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   277
  (empty(game) % MaxValue, empty(game) / MaxValue)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   278
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   279
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   280
def get_row(game: String, y: Int) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   281
  indexes.map(col => game(y * MaxValue + col))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   282
def get_col(game: String, x: Int) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   283
  indexes.map(row => game(x + row * MaxValue))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   284
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   285
def get_box(game: String, pos: Pos): List[Char] = {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   286
    def base(p: Int): Int = (p / 3) * 3
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   287
    val x0 = base(pos._1)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   288
    val y0 = base(pos._2)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   289
    val ys = (y0 until y0 + 3).toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   290
    (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
   291
}
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
// this is not mutable!!
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   294
def update(game: String, pos: Int, value: Char): String = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   295
  game.updated(pos, value)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   296
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   297
def toAvoid(game: String, pos: Pos): List[Char] = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   298
  (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
   299
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   300
def candidates(game: String, pos: Pos): List[Char] = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   301
  allValues.diff(toAvoid(game,pos))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   302
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   303
//candidates(game0, (0,0))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   304
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   305
def pretty(game: String): String = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   306
  "\n" + (game sliding (MaxValue, MaxValue) mkString "\n")
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   307
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   308
/////////////////////
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   309
// not tail recursive 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   310
def search(game: String): List[String] = {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   311
  if (isDone(game)) List(game)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   312
  else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   313
    val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   314
    cs.map(c => search(update(game, empty(game), c))).toList.flatten
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   315
  }
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   316
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   317
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   318
// tail recursive version that searches 
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   319
// for all solutions
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   320
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   321
def searchT(games: List[String], sols: List[String]): List[String] = games match {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   322
  case Nil => sols
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   323
  case game::rest => {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   324
    if (isDone(game)) searchT(rest, game::sols)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   325
    else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   326
      val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   327
      searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   328
    }
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   329
  }
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   330
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   331
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   332
searchT(List(game3), List()).map(pretty)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   333
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   334
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   335
// tail recursive version that searches 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   336
// for a single solution
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   337
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   338
def search1T(games: List[String]): Option[String] = games match {
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   339
  case Nil => None
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   340
  case game::rest => {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   341
    if (isDone(game)) Some(game)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   342
    else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   343
      val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   344
      search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   345
    }
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   346
  }
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   347
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   348
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   349
search1T(List(game3)).map(pretty)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   350
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   351
// game with multiple solutions
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   352
val game3 = """.8...9743
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   353
              |.5...8.1.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   354
              |.1.......
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   355
              |8....5...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   356
              |...8.4...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   357
              |...3....6
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   358
              |.......7.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   359
              |.3.5...8.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   360
              |9724...5.""".stripMargin.replaceAll("\\n", "")
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   361
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   362
searchT(List(game3), Nil).map(pretty)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   363
search1T(List(game3)).map(pretty)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   364
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   365
// Moral: Whenever a recursive function is resource-critical
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   366
// (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
   367
// write it in tail-recursive fashion.
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   368
// 
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   369
// Unfortuantely, Scala because of current limitations in 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   370
// 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
   371
// 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
   372
// 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
   373
// nothing is perfect. 
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   374
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   375
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   376
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   377
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   378
// Polymorphic Types
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   379
//===================
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   380
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   381
// You do not want to write functions like contains, first 
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   382
// and so on for every type of lists.
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   383
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   384
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   385
def length_string_list(lst: List[String]): Int = lst match {
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   386
  case Nil => 0
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   387
  case x::xs => 1 + length_string_list(xs)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   388
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   389
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   390
def length_int_list(lst: List[Int]): Int = lst match {
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   391
  case Nil => 0
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   392
  case x::xs => 1 + length_int_list(xs)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   393
}
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   394
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   395
length_string_list(List("1", "2", "3", "4"))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   396
length_int_list(List(1, 2, 3, 4))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   397
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   398
//-----
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   399
def length[A](lst: List[A]): Int = lst match {
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   400
  case Nil => 0
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   401
  case x::xs => 1 + length(xs)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   402
}
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   403
length(List("1", "2", "3", "4"))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   404
length(List(King, Knight("foo"), Clown))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   405
length(List(1, 2, 3, 4))
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   406
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   407
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
   408
  case Nil => Nil
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   409
  case x::xs => f(x)::map_int_list(xs, f) 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   410
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   411
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   412
map_int_list(List(1, 2, 3, 4), square)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   413
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   414
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   415
// Remember?
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   416
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
   417
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   418
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   419
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   420
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   421
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   422
// Cool Stuff
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   423
//============
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   424
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   425
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   426
// Implicits 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   427
//===========
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   428
//
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   429
// For example adding your own methods to Strings:
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   430
// Imagine you want to increment strings, like
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   431
//
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   432
//     "HAL".increment
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   433
//
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   434
// you can avoid ugly fudges, like a MyString, by
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   435
// using implicit conversions.
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   436
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   437
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   438
implicit class MyString(s: String) {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   439
  def increment = for (c <- s) yield (c + 1).toChar 
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   440
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   441
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   442
"HAL".increment
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   443
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   444
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   445
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   446
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   447
// Regular expressions - the power of DSLs in Scala
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   448
//==================================================
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   449
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   450
abstract class Rexp
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   451
case object ZERO extends Rexp                       // nothing
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   452
case object ONE extends Rexp                        // the empty string
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   453
case class CHAR(c: Char) extends Rexp               // a character c
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   454
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
   455
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
   456
case class STAR(r: Rexp) extends Rexp               // star         r*
67
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
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   459
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   460
// (ab)*
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   461
val r0 = STAR(SEQ(CHAR('a'), CHAR('b')))
67
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
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   464
// some convenience for typing in regular expressions
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   465
import scala.language.implicitConversions    
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   466
import scala.language.reflectiveCalls 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   467
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   468
def charlist2rexp(s: List[Char]): Rexp = s match {
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   469
  case Nil => ONE
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   470
  case c::Nil => CHAR(c)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   471
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   472
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   473
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
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
val r1 = STAR("ab")
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   477
val r2 = STAR(ALT("ab"))
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   478
val r3 = STAR(ALT("ab", "baa baa black sheep"))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   479
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   480
implicit def RexpOps (r: Rexp) = new {
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   481
  def | (s: Rexp) = ALT(r, s)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   482
  def % = STAR(r)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   483
  def ~ (s: Rexp) = SEQ(r, s)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   484
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   485
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   486
implicit def stringOps (s: String) = new {
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   487
  def | (r: Rexp) = ALT(s, r)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   488
  def | (r: String) = ALT(s, r)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   489
  def % = STAR(s)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   490
  def ~ (r: Rexp) = SEQ(s, r)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   491
  def ~ (r: String) = SEQ(s, r)
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
153
4383809c176a updated
Christian Urban <urbanc@in.tum.de>
parents: 152
diff changeset
   494
//example regular expressions
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   495
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   496
val sign = "+" | "-" | ""
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   497
val number = sign ~ digit ~ digit.% 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   498
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   499
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
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   502
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   503
// The End
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   504
//=========
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   505
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   506
// A function should do one thing, and only one thing.
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   507
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   508
// Make your variables immutable, unless there's a good 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   509
// reason not to.
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   510
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   511
// 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
   512
//
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   513
// http://scalapuzzlers.com
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   514
//
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   515
// 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
   516
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   517
List(1, 2, 3) contains "your mom"
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   518
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   519
// I like best about Scala that it lets me often write
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   520
// concise, readable code.
68
8da9e0c16194 updated
Christian Urban <urbanc@in.tum.de>
parents: 67
diff changeset
   521
170
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   522
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   523
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   524
// You can define your own while loop
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
def my_while(condition: => Boolean)(block: => Unit): Unit = 
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   528
  if (condition) { block ; my_while(condition) { block } } else { }
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   529
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   530
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   531
var x = 10
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   532
my_while (x > 0) { 
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   533
  println(s"$x") ; x = x - 1 
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   534
}
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   535
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   536
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   537
`symbol
37b1bfcdba79 updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   538
`symbol`