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