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