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