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