progs/lecture3.scala
author Christian Urban <urbanc@in.tum.de>
Tue, 12 Nov 2019 00:41:00 +0000
changeset 318 029e2862bb4e
parent 223 c6453f3547ec
child 320 cdfb2ce30a3d
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
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     4
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     5
// A Web Crawler / Email Harvester
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     6
//=================================
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     7
//
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     8
// the idea is to look for links using the
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     9
// regular expression "https?://[^"]*" and for
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
    10
// email addresses using yet another regex.
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    11
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    12
import io.Source
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    13
import scala.util._
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    14
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    15
// gets the first 10K of a web-page
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    16
def get_page(url: String) : String = {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    17
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    18
    getOrElse { println(s"  Problem with: $url"); ""}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    19
}
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    20
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    21
// regex for URLs and emails
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    22
val http_pattern = """"https?://[^"]*"""".r
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    23
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    24
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
    25
//  val s = "foo bla christian@kcl.ac.uk 1234567"
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
    26
//  email_pattern.findAllIn(s).toList
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    27
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    28
// drops the first and last character from a string
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    29
def unquote(s: String) = s.drop(1).dropRight(1)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    30
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    31
def get_all_URLs(page: String): Set[String] = 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    32
  http_pattern.findAllIn(page).map(unquote).toSet
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    33
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    34
// naive version of crawl - searches until a given depth,
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    35
// visits pages potentially more than once
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
    36
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    37
def crawl(url: String, n: Int) : Set[String] = {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    38
  if (n == 0) Set()
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    39
  else {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    40
    println(s"  Visiting: $n $url")
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    41
    val page = get_page(url)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    42
    val new_emails = email_pattern.findAllIn(page).toSet
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    43
    new_emails ++ 
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
    44
      (for (u <- get_all_URLs(page).par) yield crawl(u, n - 1)).flatten
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    45
  }
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    46
}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    47
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    48
// some starting URLs for the crawler
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    49
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    50
crawl(startURL, 2)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    51
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    52
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    53
// User-defined Datatypes
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    54
//========================
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    55
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    56
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    57
abstract class Colour
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    58
case object Red extends Colour 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    59
case object Green extends Colour 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    60
case object Blue extends Colour
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    61
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    62
def fav_colour(c: Colour) : Boolean = c match {
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    63
  case Red   => false
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    64
  case Green => true
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    65
  case Blue  => false 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    66
}
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    67
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    68
fav_colour(Green)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    69
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    70
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    71
// ... a tiny bit more useful: Roman Numerals
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    72
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    73
abstract class RomanDigit 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    74
case object I extends RomanDigit 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    75
case object V extends RomanDigit 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    76
case object X extends RomanDigit 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    77
case object L extends RomanDigit 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    78
case object C extends RomanDigit 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    79
case object D extends RomanDigit 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    80
case object M extends RomanDigit 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    81
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    82
type RomanNumeral = List[RomanDigit] 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    83
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    84
List(X,I)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    85
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    86
/*
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    87
I -> 1
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    88
II -> 2
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    89
III  -> 3
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    90
IV -> 4
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    91
V -> 5
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    92
VI -> 6
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    93
VII -> 7
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    94
VIII -> 8
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    95
IX -> 9
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    96
X -> X
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    97
*/
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    98
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
    99
def RomanNumeral2Int(rs: RomanNumeral): Int = rs match { 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   100
  case Nil => 0
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   101
  case M::r    => 1000 + RomanNumeral2Int(r)  
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   102
  case C::M::r => 900 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   103
  case D::r    => 500 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   104
  case C::D::r => 400 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   105
  case C::r    => 100 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   106
  case X::C::r => 90 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   107
  case L::r    => 50 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   108
  case X::L::r => 40 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   109
  case X::r    => 10 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   110
  case I::X::r => 9 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   111
  case V::r    => 5 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   112
  case I::V::r => 4 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   113
  case I::r    => 1 + RomanNumeral2Int(r)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   114
}
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   115
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   116
RomanNumeral2Int(List(I,V))             // 4
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   117
RomanNumeral2Int(List(I,I,I,I))         // 4 (invalid Roman number)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   118
RomanNumeral2Int(List(V,I))             // 6
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   119
RomanNumeral2Int(List(I,X))             // 9
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   120
RomanNumeral2Int(List(M,C,M,L,X,X,I,X)) // 1979
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   121
RomanNumeral2Int(List(M,M,X,V,I,I))     // 2017
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   122
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   123
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   124
// another example
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   125
//=================
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   126
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   127
// Once upon a time, in a complete fictional 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   128
// country there were Persons...
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   129
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   130
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   131
abstract class Person
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   132
case object King extends Person
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   133
case class Peer(deg: String, terr: String, succ: Int) extends Person
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   134
case class Knight(name: String) extends Person
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   135
case class Peasant(name: String) extends Person
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   136
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   137
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   138
def title(p: Person): String = p match {
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   139
  case King => "His Majesty the King"
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   140
  case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   141
  case Knight(name) => s"Sir ${name}"
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   142
  case Peasant(name) => name
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   143
}
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   144
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   145
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   146
  case (King, _) => true
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   147
  case (Peer(_,_,_), Knight(_)) => true
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   148
  case (Peer(_,_,_), Peasant(_)) => true
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   149
  case (Peer(_,_,_), Clown) => true
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   150
  case (Knight(_), Peasant(_)) => true
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   151
  case (Knight(_), Clown) => true
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   152
  case (Clown, Peasant(_)) => true
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   153
  case _ => false
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   154
}
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   155
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   156
val people = List(Knight("David"), 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   157
                  Peer("Duke", "Norfolk", 84), 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   158
                  Peasant("Christian"), 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   159
                  King, 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   160
                  Clown)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   161
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   162
println(people.sortWith(superior).mkString("\n"))
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   163
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   164
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   165
// String interpolations as patterns
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   166
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   167
val date = "2000-01-01"
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   168
val s"$year-$month-$day" = date
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   169
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   170
def parse_date(date: String) = date match {
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   171
  case s"$year-$month-$day" => Some((year.toInt, month.toInt, day.toInt))
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   172
  case s"$day/$month/$year" => Some((year.toInt, month.toInt, day.toInt))
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   173
  case _ => None
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   174
} 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   175
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   176
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   177
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   178
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   179
// User-defined Datatypes and Pattern Matching
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   180
//=============================================
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   181
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   182
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   183
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   184
abstract class Exp
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   185
case class N(n: Int) extends Exp                  // for numbers
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   186
case class Plus(e1: Exp, e2: Exp) extends Exp
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   187
case class Times(e1: Exp, e2: Exp) extends Exp
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   188
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   189
def string(e: Exp) : String = e match {
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   190
  case N(n) => n.toString
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   191
  case Plus(e1, e2) => "(" + string(e1) + " + " + string(e2) + ")" 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   192
  case Times(e1, e2) => "(" + string(e1) + " * " + string(e2) + ")" 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   193
}
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   194
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   195
val e = Plus(N(9), Times(N(3), N(4)))
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   196
println(string(e))
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   197
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   198
def eval(e: Exp) : Int = e match {
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   199
  case N(n) => n
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   200
  case Plus(e1, e2) => eval(e1) + eval(e2) 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   201
  case Times(e1, e2) => eval(e1) * eval(e2) 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   202
}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   203
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   204
def simp(e: Exp) : Exp = e match {
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   205
  case N(n) => N(n)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   206
  case Plus(e1, e2) => (simp(e1), simp(e2)) match {
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   207
    case (N(0), e2s) => e2s
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   208
    case (e1s, N(0)) => e1s
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   209
    case (e1s, e2s) => Plus(e1s, e2s)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   210
  }  
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   211
  case Times(e1, e2) => (simp(e1), simp(e2)) match {
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   212
    case (N(0), _) => N(0)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   213
    case (_, N(0)) => N(0)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   214
    case (N(1), e2s) => e2s
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   215
    case (e1s, N(1)) => e1s
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   216
    case (e1s, e2s) => Times(e1s, e2s)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   217
  }  
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   218
}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   219
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   220
println(eval(e))
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   221
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   222
val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9)))
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   223
println(string(e2))
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   224
println(string(simp(e2)))
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   225
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   226
// Tokens and Reverse Polish Notation
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   227
abstract class Token
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   228
case class T(n: Int) extends Token
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   229
case object PL extends Token
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   230
case object TI extends Token
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   231
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   232
def rp(e: Exp) : List[Token] = e match {
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   233
  case N(n) => List(T(n))
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   234
  case Plus(e1, e2) => rp(e1) ::: rp(e2) ::: List(PL) 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   235
  case Times(e1, e2) => rp(e1) ::: rp(e2) ::: List(TI) 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   236
}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   237
println(string(e2))
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   238
println(rp(e2))
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   239
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   240
def comp(ls: List[Token], st: List[Int]) : Int = (ls, st) match {
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   241
  case (Nil, st) => st.head 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   242
  case (T(n)::rest, st) => comp(rest, n::st)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   243
  case (PL::rest, n1::n2::st) => comp(rest, n1 + n2::st)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   244
  case (TI::rest, n1::n2::st) => comp(rest, n1 * n2::st)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   245
}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   246
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   247
comp(rp(e), Nil)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   248
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   249
def proc(s: String) : Token = s match {
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   250
  case  "+" => PL
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   251
  case  "*" => TI
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   252
  case  _ => T(s.toInt) 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   253
}
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   254
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   255
comp("1 2 + 4 * 5 + 3 +".split(" ").toList.map(proc), Nil)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   256
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   257
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   258
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   259
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   260
def string(e: Exp) : String = e match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   261
  case N(n) => n.toString
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   262
  case Plus(e1, e2) => "(" + string(e1) + " + " + string(e2) + ")"
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   263
  case Times(e1, e2) => "(" + string(e1) + " * " + string(e2) + ")"
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   264
}
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   265
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   266
val e = Plus(N(9), Times(N(3), N(4)))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   267
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   268
println(string(e))
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   269
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   270
def eval(e: Exp) : Int = e match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   271
  case N(n) => n
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   272
  case Plus(e1, e2) => eval(e1) + eval(e2)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   273
  case Times(e1, e2) => eval(e1) * eval(e2)
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   274
}
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   275
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   276
eval(e)
178
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   277
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   278
def simp(e: Exp) : Exp = e match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   279
  case N(n) => N(n)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   280
  case Plus(e1, e2) => (simp(e1), simp(e2)) match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   281
    case (N(0), e2s) => e2s
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   282
    case (e1s, N(0)) => e1s
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   283
    case (e1s, e2s) => Plus(e1s, e2s) 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   284
  }
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   285
  case Times(e1, e2) => (simp(e1), simp(e2)) match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   286
    case (N(0), e2s) => N(0)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   287
    case (e1s, N(0)) => N(0)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   288
    case (N(1), e2s) => e2s
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   289
    case (e1s, N(1)) => e1s
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   290
    case (e1s, e2s) => Times(e1s, e2s) 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   291
  }
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   292
}
178
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   293
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   294
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   295
val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9)))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   296
println(string(e2))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   297
println(string(simp(e2)))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   298
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   299
// Token and Reverse Polish Notation
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   300
abstract class Token
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   301
case class T(n: Int) extends Token
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   302
case object PL extends Token
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   303
case object TI extends Token
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   304
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   305
def rp(e: Exp) : List[Token] = e match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   306
  case N(n) => List(T(n))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   307
  case Plus(e1, e2) => rp(e1) ::: rp(e2) ::: List(PL)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   308
  case Times(e1, e2) => rp(e1) ::: rp(e2) ::: List(TI)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   309
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   310
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   311
def comp(ts: List[Token], stk: List[Int]) : Int = (ts, stk) match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   312
  case (Nil, st) => st.head
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   313
  case (T(n)::rest, st) => comp(rest, n::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   314
  case (PL::rest, n1::n2::st) => comp(rest, n1 + n2::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   315
  case (TI::rest, n1::n2::st) => comp(rest, n1 * n2::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   316
}
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   317
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   318
def exp(ts: List[Token], st: List[Exp]) : Exp = (ts, st) match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   319
  case (Nil, st) => st.head
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   320
  case (T(n)::rest, st) => exp(rest, N(n)::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   321
  case (PL::rest, n1::n2::st) => exp(rest, Plus(n2, n1)::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   322
  case (TI::rest, n1::n2::st) => exp(rest, Times(n2, n1)::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   323
}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   324
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   325
exp(toks(e2), Nil)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   326
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   327
def proc(s: String) = s match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   328
  case "+" => PL
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   329
  case "*" => TI
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   330
  case n => T(n.toInt)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   331
}
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   332
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   333
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   334
string(exp("1 2 + 4 * 5 + 3 +".split(" ").toList.map(proc), Nil))
67
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
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   337
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   338
// Tail recursion
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   339
//================
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   340
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   341
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   342
def fact(n: Long): Long = 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   343
  if (n == 0) 1 else n * fact(n - 1)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   344
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   345
def factB(n: BigInt): BigInt = 
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   346
  if (n == 0) 1 else n * factB(n - 1)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   347
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   348
factB(100000)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   349
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   350
fact(10)              //ok
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   351
fact(10000)           // produces a stackoverflow
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   352
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   353
def factT(n: BigInt, acc: BigInt): BigInt =
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   354
  if (n == 0) acc else factT(n - 1, n * acc)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   355
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   356
factT(10, 1)
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   357
println(factT(100000, 1))
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   358
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   359
// there is a flag for ensuring a function is tail recursive
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   360
import scala.annotation.tailrec
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   361
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   362
@tailrec
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   363
def factT(n: BigInt, acc: BigInt): BigInt =
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   364
  if (n == 0) acc else factT(n - 1, n * acc)
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
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   367
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   368
// for tail-recursive functions the Scala compiler
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   369
// generates loop-like code, which does not need
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   370
// to allocate stack-space in each recursive
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   371
// call; Scala can do this only for tail-recursive
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   372
// functions
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
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   375
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   376
// Jumping Towers
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   377
//================
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   378
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   379
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   380
// the first n prefixes of xs
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   381
// for 1 => include xs
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   382
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   383
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   384
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   385
def moves(xs: List[Int], n: Int) : List[List[Int]] = (xs, n) match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   386
  case (Nil, _) => Nil
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   387
  case (xs, 0) => Nil
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   388
  case (x::xs, n) => (x::xs) :: moves(xs, n - 1)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   389
}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   390
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   391
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   392
moves(List(5,1,0), 1)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   393
moves(List(5,1,0), 2)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   394
moves(List(5,1,0), 5)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   395
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   396
// checks whether a jump tour exists at all
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   397
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   398
def search(xs: List[Int]) : Boolean = xs match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   399
  case Nil => true
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   400
  case (x::xs) =>
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   401
    if (xs.length < x) true else moves(xs, x).exists(search(_))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   402
}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   403
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   404
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   405
search(List(5,3,2,5,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   406
search(List(3,5,1,0,0,0,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   407
search(List(3,5,1,0,0,0,0,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   408
search(List(3,5,1,0,0,0,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   409
search(List(3,5,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   410
search(List(5,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   411
search(Nil)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   412
search(List(1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   413
search(List(5,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   414
search(List(3,5,1,0,0,0,0,0,0,0,0,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   415
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   416
// generates *all* jump tours
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   417
//    if we are only interested in the shortes one, we could
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   418
//    shortcircut the calculation and only return List(x) in
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   419
//    case where xs.length < x, because no tour can be shorter
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   420
//    than 1
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   421
// 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   422
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   423
def jumps(xs: List[Int]) : List[List[Int]] = xs match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   424
  case Nil => Nil
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   425
  case (x::xs) => {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   426
    val children = moves(xs, x)
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   427
    val results = children.map((cs) => jumps(cs).map(x :: _)).flatten
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   428
    if (xs.length < x) List(x) :: results else results
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   429
  }
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   430
}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   431
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   432
println(jumps(List(5,3,2,5,1,1)).minBy(_.length))
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   433
jumps(List(3,5,1,2,1,2,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   434
jumps(List(3,5,1,2,3,4,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   435
jumps(List(3,5,1,0,0,0,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   436
jumps(List(3,5,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   437
jumps(List(5,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   438
jumps(Nil)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   439
jumps(List(1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   440
jumps(List(5,1,2))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   441
moves(List(1,2), 5)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   442
jumps(List(1,5,1,2))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   443
jumps(List(3,5,1,0,0,0,0,0,0,0,0,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   444
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   445
jumps(List(5,3,2,5,1,1)).minBy(_.length)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   446
jumps(List(1,3,5,8,9,2,6,7,6,8,9)).minBy(_.length)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   447
jumps(List(1,3,6,1,0,9)).minBy(_.length)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   448
jumps(List(2,3,1,1,2,4,2,0,1,1)).minBy(_.length)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   449
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   450
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   451
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   452
// Tail Recursion
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   453
//================
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   454
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   455
318
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   456
def fact(n: Long): Long = 
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   457
  if (n == 0) 1 else n * fact(n - 1)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   458
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   459
fact(10)              //ok
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   460
fact(10000)           // produces a stackoverflow
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   461
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   462
def factT(n: BigInt, acc: BigInt): BigInt =
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   463
  if (n == 0) acc else factT(n - 1, n * acc)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   464
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   465
factT(10, 1)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   466
factT(100000, 1)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   467
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   468
// there is a flag for ensuring a function is tail recursive
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   469
import scala.annotation.tailrec
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   470
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   471
@tailrec
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   472
def factT(n: BigInt, acc: BigInt): BigInt =
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   473
  if (n == 0) acc else factT(n - 1, n * acc)
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   474
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   475
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   476
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   477
// for tail-recursive functions the Scala compiler
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   478
// generates loop-like code, which does not need
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   479
// to allocate stack-space in each recursive
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   480
// call; Scala can do this only for tail-recursive
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   481
// functions
029e2862bb4e updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   482
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   483
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   484
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   485
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   486
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   487
// Sudoku 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   488
//========
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   489
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   490
// THE POINT OF THIS CODE IS NOT TO BE SUPER
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   491
// EFFICIENT AND FAST, just explaining exhaustive
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   492
// depth-first search
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   493
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   494
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   495
val game0 = """.14.6.3..
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   496
              |62...4..9
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   497
              |.8..5.6..
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   498
              |.6.2....3
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   499
              |.7..1..5.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   500
              |5....9.6.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   501
              |..6.2..3.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   502
              |1..5...92
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   503
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   504
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   505
type Pos = (Int, Int)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   506
val EmptyValue = '.'
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   507
val MaxValue = 9
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   508
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   509
val allValues = "123456789".toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   510
val indexes = (0 to 8).toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   511
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   512
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   513
def empty(game: String) = game.indexOf(EmptyValue)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   514
def isDone(game: String) = empty(game) == -1 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   515
def emptyPosition(game: String) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   516
  (empty(game) % MaxValue, empty(game) / MaxValue)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   517
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   518
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   519
def get_row(game: String, y: Int) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   520
  indexes.map(col => game(y * MaxValue + col))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   521
def get_col(game: String, x: Int) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   522
  indexes.map(row => game(x + row * MaxValue))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   523
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   524
def get_box(game: String, pos: Pos): List[Char] = {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   525
    def base(p: Int): Int = (p / 3) * 3
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   526
    val x0 = base(pos._1)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   527
    val y0 = base(pos._2)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   528
    val ys = (y0 until y0 + 3).toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   529
    (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
   530
}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   531
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   532
//get_row(game0, 0)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   533
//get_row(game0, 1)
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   534
//get_col(game0, 0)
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   535
//get_box(game0, (3, 1))
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   536
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   537
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   538
// this is not mutable!!
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   539
def update(game: String, pos: Int, value: Char): String = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   540
  game.updated(pos, value)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   541
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   542
def toAvoid(game: String, pos: Pos): List[Char] = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   543
  (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
   544
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   545
def candidates(game: String, pos: Pos): List[Char] = 
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   546
  allValues.diff(toAvoid(game, pos))
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   547
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   548
//candidates(game0, (0,0))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   549
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   550
def pretty(game: String): String = 
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   551
  "\n" + (game.sliding(MaxValue, MaxValue).mkString("\n"))
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   552
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   553
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   554
def search(game: String): List[String] = {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   555
  if (isDone(game)) List(game)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   556
  else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   557
    val cs = candidates(game, emptyPosition(game))
218
22705d22c105 updated
Christian Urban <urbanc@in.tum.de>
parents: 217
diff changeset
   558
    cs.par.map(c => search(update(game, empty(game), c))).toList.flatten
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   559
  }
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   560
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   561
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   562
search(game0).map(pretty)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   563
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   564
val game1 = """23.915...
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   565
              |...2..54.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   566
              |6.7......
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   567
              |..1.....9
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   568
              |89.5.3.17
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   569
              |5.....6..
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   570
              |......9.5
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   571
              |.16..7...
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   572
              |...329..1""".stripMargin.replaceAll("\\n", "")
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   573
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   574
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   575
// game that is in the hard category
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   576
val game2 = """8........
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   577
              |..36.....
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   578
              |.7..9.2..
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   579
              |.5...7...
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   580
              |....457..
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   581
              |...1...3.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   582
              |..1....68
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   583
              |..85...1.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   584
              |.9....4..""".stripMargin.replaceAll("\\n", "")
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   585
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   586
// game with multiple solutions
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   587
val game3 = """.8...9743
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   588
              |.5...8.1.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   589
              |.1.......
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   590
              |8....5...
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   591
              |...8.4...
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   592
              |...3....6
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   593
              |.......7.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   594
              |.3.5...8.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   595
              |9724...5.""".stripMargin.replaceAll("\\n", "")
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   596
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   597
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   598
search(game1).map(pretty)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   599
search(game3).map(pretty)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   600
search(game2).map(pretty)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   601
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   602
// for measuring time
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   603
def time_needed[T](i: Int, code: => T) = {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   604
  val start = System.nanoTime()
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   605
  for (j <- 1 to i) code
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   606
  val end = System.nanoTime()
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   607
  ((end - start) / 1.0e9) + " secs"
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   608
}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   609
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   610
time_needed(1, search(game2))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   611
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   612
// tail recursive version that searches 
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   613
// for all solutions
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   614
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   615
def searchT(games: List[String], sols: List[String]): List[String] = games match {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   616
  case Nil => sols
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   617
  case game::rest => {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   618
    if (isDone(game)) searchT(rest, game::sols)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   619
    else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   620
      val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   621
      searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   622
    }
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   623
  }
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   624
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   625
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   626
searchT(List(game3), List()).map(pretty)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   627
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   628
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   629
// tail recursive version that searches 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   630
// for a single solution
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   631
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   632
def search1T(games: List[String]): Option[String] = games match {
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   633
  case Nil => None
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   634
  case game::rest => {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   635
    if (isDone(game)) Some(game)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   636
    else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   637
      val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   638
      search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   639
    }
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   640
  }
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   641
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   642
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   643
search1T(List(game3)).map(pretty)
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   644
time_needed(10, search1T(List(game3)))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   645
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   646
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   647
// game with multiple solutions
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   648
val game3 = """.8...9743
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   649
              |.5...8.1.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   650
              |.1.......
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   651
              |8....5...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   652
              |...8.4...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   653
              |...3....6
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   654
              |.......7.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   655
              |.3.5...8.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   656
              |9724...5.""".stripMargin.replaceAll("\\n", "")
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   657
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   658
searchT(List(game3), Nil).map(pretty)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   659
search1T(List(game3)).map(pretty)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   660
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   661
// Moral: Whenever a recursive function is resource-critical
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   662
// (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
   663
// write it in tail-recursive fashion.
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   664
// 
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   665
// Unfortuantely, Scala because of current limitations in 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   666
// 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
   667
// 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
   668
// 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
   669
// nothing is perfect. 
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   670
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   671
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   672
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   673
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   674
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   675