progs/lecture3.scala
author Christian Urban <urbanc@in.tum.de>
Fri, 23 Nov 2018 01:52:37 +0000
changeset 217 e689375abcc1
parent 194 060b081523de
child 218 22705d22c105
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     1
// Scala Lecture 3
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     2
//=================
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
     3
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     4
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     5
// A Web Crawler / Email Harvester
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     6
//=================================
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     7
//
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     8
// the idea is to look for links using the
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
     9
// regular expression "https?://[^"]*" and for
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    10
// email addresses using another regex.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    11
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    12
import io.Source
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    13
import scala.util._
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    14
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    15
// gets the first 10K of a web-page
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    16
def get_page(url: String) : String = {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    17
  Try(Source.fromURL(url)("ISO-8859-1").take(10000).mkString).
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    18
    getOrElse { println(s"  Problem with: $url"); ""}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    19
}
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    20
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    21
// regex for URLs and emails
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    22
val http_pattern = """"https?://[^"]*"""".r
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    23
val email_pattern = """([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})""".r
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    24
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    25
//email_pattern.findAllIn
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    26
//  ("foo bla christian@kcl.ac.uk 1234567").toList
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    27
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    28
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    29
// drops the first and last character from a string
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    30
def unquote(s: String) = s.drop(1).dropRight(1)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    31
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    32
def get_all_URLs(page: String): Set[String] = 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    33
  http_pattern.findAllIn(page).map(unquote).toSet
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    34
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    35
// naive version of crawl - searches until a given depth,
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    36
// visits pages potentially more than once
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    37
def crawl(url: String, n: Int) : Set[String] = {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    38
  if (n == 0) Set()
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    39
  else {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    40
    println(s"  Visiting: $n $url")
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    41
    val page = get_page(url)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    42
    val new_emails = email_pattern.findAllIn(page).toSet
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    43
    new_emails ++ 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    44
      (for (u <- get_all_URLs(page)) yield crawl(u, n - 1)).flatten
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    45
  }
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    46
}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    47
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    48
// some starting URLs for the crawler
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    49
val startURL = """https://nms.kcl.ac.uk/christian.urban/"""
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    50
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    51
crawl(startURL, 2)
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
// User-defined Datatypes and Pattern Matching
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    56
//============================================
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    57
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    58
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    59
abstract class Exp
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    60
case class N(n: Int) extends Exp
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    61
case class Plus(e1: Exp, e2: Exp) extends Exp
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    62
case class Times(e1: Exp, e2: Exp) extends Exp
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
    63
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    64
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    65
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    66
// string of an Exp
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    67
// eval of an Exp
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    68
// simp an Exp
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    69
// Tokens
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    70
// Reverse Polish Notation
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    71
// compute RP
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    72
// transform RP into Exp
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    73
// process RP string and generate Exp
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    74
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    75
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    76
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    77
def string(e: Exp) : String = e match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    78
  case N(n) => n.toString
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    79
  case Plus(e1, e2) => "(" + string(e1) + " + " + string(e2) + ")"
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    80
  case Times(e1, e2) => "(" + string(e1) + " * " + string(e2) + ")"
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    81
}
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    82
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    83
val e = Plus(N(9), Times(N(3), N(4)))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    84
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    85
println(string(e))
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
    86
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    87
def eval(e: Exp) : Int = e match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    88
  case N(n) => n
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    89
  case Plus(e1, e2) => eval(e1) + eval(e2)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    90
  case Times(e1, e2) => eval(e1) * eval(e2)
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    91
}
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
    92
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    93
eval(e)
178
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
    94
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    95
def simp(e: Exp) : Exp = e match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    96
  case N(n) => N(n)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    97
  case Plus(e1, e2) => (simp(e1), simp(e2)) match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    98
    case (N(0), e2s) => e2s
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
    99
    case (e1s, N(0)) => e1s
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   100
    case (e1s, e2s) => Plus(e1s, e2s) 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   101
  }
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   102
  case Times(e1, e2) => (simp(e1), simp(e2)) match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   103
    case (N(0), e2s) => N(0)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   104
    case (e1s, N(0)) => N(0)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   105
    case (N(1), e2s) => e2s
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   106
    case (e1s, N(1)) => e1s
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   107
    case (e1s, e2s) => Times(e1s, e2s) 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   108
  }
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   109
}
178
fdf77ee57cdc updated
Christian Urban <urbanc@in.tum.de>
parents: 170
diff changeset
   110
152
114a89518aea updated
Christian Urban <urbanc@in.tum.de>
parents: 77
diff changeset
   111
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   112
val e2 = Times(Plus(N(0), N(1)), Plus(N(0), N(9)))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   113
println(string(e2))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   114
println(string(simp(e2)))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   115
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   116
// Token and Reverse Polish Notation
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   117
abstract class Token
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   118
case class T(n: Int) extends Token
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   119
case object PL extends Token
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   120
case object TI extends Token
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   121
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   122
def rp(e: Exp) : List[Token] = e match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   123
  case N(n) => List(T(n))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   124
  case Plus(e1, e2) => rp(e1) ::: rp(e2) ::: List(PL)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   125
  case Times(e1, e2) => rp(e1) ::: rp(e2) ::: List(TI)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   126
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   127
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   128
def comp(ts: List[Token], stk: List[Int]) : Int = (ts, stk) match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   129
  case (Nil, st) => st.head
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   130
  case (T(n)::rest, st) => comp(rest, n::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   131
  case (PL::rest, n1::n2::st) => comp(rest, n1 + n2::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   132
  case (TI::rest, n1::n2::st) => comp(rest, n1 * n2::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   133
}
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   134
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   135
def exp(ts: List[Token], st: List[Exp]) : Exp = (ts, st) match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   136
  case (Nil, st) => st.head
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   137
  case (T(n)::rest, st) => exp(rest, N(n)::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   138
  case (PL::rest, n1::n2::st) => exp(rest, Plus(n2, n1)::st)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   139
  case (TI::rest, n1::n2::st) => exp(rest, Times(n2, n1)::st)
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
exp(toks(e2), Nil)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   143
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   144
def proc(s: String) = s match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   145
  case "+" => PL
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   146
  case "*" => TI
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   147
  case n => T(n.toInt)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   148
}
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   149
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   150
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   151
string(exp("1 2 + 4 * 5 + 3 +".split(" ").toList.map(proc), Nil))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   152
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   153
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   154
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   155
// Tail recursion
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   156
//================
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   157
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   158
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   159
def fact(n: Long): Long = 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   160
  if (n == 0) 1 else n * fact(n - 1)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   161
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   162
fact(10)              //ok
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   163
fact(10000)           // produces a stackoverflow
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   164
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   165
def factT(n: BigInt, acc: BigInt): BigInt =
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   166
  if (n == 0) acc else factT(n - 1, n * acc)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   167
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   168
factT(10, 1)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   169
factT(100000, 1)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   170
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   171
// there is a flag for ensuring a function is tail recursive
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   172
import scala.annotation.tailrec
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   173
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   174
@tailrec
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   175
def factT(n: BigInt, acc: BigInt): BigInt =
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   176
  if (n == 0) acc else factT(n - 1, n * acc)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   177
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   178
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   179
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   180
// for tail-recursive functions the Scala compiler
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   181
// generates loop-like code, which does not need
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   182
// to allocate stack-space in each recursive
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   183
// call; Scala can do this only for tail-recursive
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   184
// functions
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   185
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   186
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   187
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   188
// Jumping Towers
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   189
//================
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   190
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   191
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   192
// the first n prefixes of xs
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   193
// for 1 => include xs
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   194
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   195
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
   196
  case (Nil, _) => Nil
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   197
  case (xs, 0) => Nil
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   198
  case (x::xs, n) => (x::xs) :: moves(xs, n - 1)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   199
}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   200
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
moves(List(5,1,0), 1)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   203
moves(List(5,1,0), 2)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   204
moves(List(5,1,0), 5)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   205
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   206
// checks whether a jump tour exists at all
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   207
// in the second case it needs to be < instead of <=
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   208
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   209
def search(xs: List[Int]) : Boolean = xs match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   210
  case Nil => true
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   211
  case (x::xs) =>
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   212
    if (xs.length < x) true else moves(xs, x).exists(search(_))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   213
}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   214
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   215
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   216
search(List(5,3,2,5,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   217
search(List(3,5,1,0,0,0,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   218
search(List(3,5,1,0,0,0,0,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   219
search(List(3,5,1,0,0,0,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   220
search(List(3,5,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   221
search(List(5,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   222
search(Nil)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   223
search(List(1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   224
search(List(5,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   225
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
   226
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   227
// generates *all* jump tours
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   228
//    if we are only interested in the shortes one, we could
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   229
//    shortcircut the calculation and only return List(x) in
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   230
//    case where xs.length < x, because no tour can be shorter
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   231
//    than 1
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   232
// 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   233
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   234
def jumps(xs: List[Int]) : List[List[Int]] = xs match {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   235
  case Nil => Nil
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   236
  case (x::xs) => {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   237
    val children = moves(xs, x)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   238
    val results = children.flatMap((cs) => jumps(cs).map(x :: _))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   239
    if (xs.length < x) List(x) :: results else results
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   240
  }
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   241
}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   242
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   243
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   244
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   245
jumps(List(5,3,2,5,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   246
jumps(List(3,5,1,2,1,2,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   247
jumps(List(3,5,1,2,3,4,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   248
jumps(List(3,5,1,0,0,0,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   249
jumps(List(3,5,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   250
jumps(List(5,1,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   251
jumps(Nil)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   252
jumps(List(1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   253
jumps(List(5,1,2))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   254
moves(List(1,2), 5)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   255
jumps(List(1,5,1,2))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   256
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
   257
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   258
jumps(List(5,3,2,5,1,1)).minBy(_.length)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   259
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
   260
jumps(List(1,3,6,1,0,9)).minBy(_.length)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   261
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
   262
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   263
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   264
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   265
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   266
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   267
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   268
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   269
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   270
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   271
// Sudoku 
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   272
//========
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   273
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   274
// THE POINT OF THIS CODE IS NOT TO BE SUPER
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   275
// EFFICIENT AND FAST, just explaining exhaustive
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   276
// depth-first search
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   277
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   278
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   279
val game0 = """.14.6.3..
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   280
              |62...4..9
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   281
              |.8..5.6..
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   282
              |.6.2....3
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   283
              |.7..1..5.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   284
              |5....9.6.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   285
              |..6.2..3.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   286
              |1..5...92
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   287
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   288
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   289
type Pos = (Int, Int)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   290
val EmptyValue = '.'
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   291
val MaxValue = 9
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   292
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   293
val allValues = "123456789".toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   294
val indexes = (0 to 8).toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   295
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   296
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   297
def empty(game: String) = game.indexOf(EmptyValue)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   298
def isDone(game: String) = empty(game) == -1 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   299
def emptyPosition(game: String) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   300
  (empty(game) % MaxValue, empty(game) / MaxValue)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   301
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   302
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   303
def get_row(game: String, y: Int) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   304
  indexes.map(col => game(y * MaxValue + col))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   305
def get_col(game: String, x: Int) = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   306
  indexes.map(row => game(x + row * MaxValue))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   307
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   308
def get_box(game: String, pos: Pos): List[Char] = {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   309
    def base(p: Int): Int = (p / 3) * 3
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   310
    val x0 = base(pos._1)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   311
    val y0 = base(pos._2)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   312
    val ys = (y0 until y0 + 3).toList
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   313
    (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
   314
}
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   315
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   316
//get_row(game0, 0)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   317
//get_row(game0, 1)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   318
//get_box(game0, (3,1))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   319
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   320
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   321
// this is not mutable!!
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   322
def update(game: String, pos: Int, value: Char): String = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   323
  game.updated(pos, value)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   324
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   325
def toAvoid(game: String, pos: Pos): List[Char] = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   326
  (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
   327
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   328
def candidates(game: String, pos: Pos): List[Char] = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   329
  allValues.diff(toAvoid(game,pos))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   330
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   331
//candidates(game0, (0,0))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   332
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   333
def pretty(game: String): String = 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   334
  "\n" + (game sliding (MaxValue, MaxValue) mkString "\n")
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   335
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   336
/////////////////////
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   337
// not tail recursive 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   338
def search(game: String): List[String] = {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   339
  if (isDone(game)) List(game)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   340
  else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   341
    val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   342
    cs.map(c => search(update(game, empty(game), c))).toList.flatten
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   343
  }
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   344
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   345
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   346
search(game0).map(pretty)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   347
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   348
val game1 = """23.915...
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   349
              |...2..54.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   350
              |6.7......
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   351
              |..1.....9
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   352
              |89.5.3.17
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   353
              |5.....6..
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   354
              |......9.5
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   355
              |.16..7...
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   356
              |...329..1""".stripMargin.replaceAll("\\n", "")
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   357
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   358
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   359
// game that is in the hard category
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   360
val game2 = """8........
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   361
              |..36.....
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   362
              |.7..9.2..
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   363
              |.5...7...
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   364
              |....457..
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   365
              |...1...3.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   366
              |..1....68
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   367
              |..85...1.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   368
              |.9....4..""".stripMargin.replaceAll("\\n", "")
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   369
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   370
// game with multiple solutions
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   371
val game3 = """.8...9743
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   372
              |.5...8.1.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   373
              |.1.......
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   374
              |8....5...
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   375
              |...8.4...
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   376
              |...3....6
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   377
              |.......7.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   378
              |.3.5...8.
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   379
              |9724...5.""".stripMargin.replaceAll("\\n", "")
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   380
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   381
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   382
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
search(game1).map(pretty)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   385
search(game3).map(pretty)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   386
search(game2).map(pretty)
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   387
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   388
// for measuring time
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   389
def time_needed[T](i: Int, code: => T) = {
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   390
  val start = System.nanoTime()
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   391
  for (j <- 1 to i) code
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   392
  val end = System.nanoTime()
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   393
  ((end - start) / 1.0e9) + " secs"
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   394
}
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   395
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   396
time_needed(1, search(game2))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   397
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   398
// tail recursive version that searches 
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   399
// for all solutions
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   400
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   401
def searchT(games: List[String], sols: List[String]): List[String] = games match {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   402
  case Nil => sols
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   403
  case game::rest => {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   404
    if (isDone(game)) searchT(rest, game::sols)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   405
    else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   406
      val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   407
      searchT(cs.map(c => update(game, empty(game), c)) ::: rest, sols)
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
  }
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   410
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   411
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   412
searchT(List(game3), List()).map(pretty)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   413
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   414
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   415
// tail recursive version that searches 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   416
// for a single solution
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   417
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   418
def search1T(games: List[String]): Option[String] = games match {
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   419
  case Nil => None
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   420
  case game::rest => {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   421
    if (isDone(game)) Some(game)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   422
    else {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   423
      val cs = candidates(game, emptyPosition(game))
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   424
      search1T(cs.map(c => update(game, empty(game), c)) ::: rest)
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   425
    }
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   426
  }
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   427
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   428
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   429
search1T(List(game3)).map(pretty)
217
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   430
time_needed(10, search1T(List(game3)))
e689375abcc1 updated
Christian Urban <urbanc@in.tum.de>
parents: 194
diff changeset
   431
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   432
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   433
// game with multiple solutions
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   434
val game3 = """.8...9743
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   435
              |.5...8.1.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   436
              |.1.......
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   437
              |8....5...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   438
              |...8.4...
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   439
              |...3....6
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   440
              |.......7.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   441
              |.3.5...8.
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   442
              |9724...5.""".stripMargin.replaceAll("\\n", "")
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   443
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   444
searchT(List(game3), Nil).map(pretty)
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   445
search1T(List(game3)).map(pretty)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   446
77
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   447
// Moral: Whenever a recursive function is resource-critical
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   448
// (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
   449
// write it in tail-recursive fashion.
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   450
// 
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   451
// Unfortuantely, Scala because of current limitations in 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   452
// 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
   453
// 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
   454
// 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
   455
// nothing is perfect. 
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   456
3cbe3d90b77f updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
   457
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   458
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   459
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   460
// Polymorphic Types
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   461
//===================
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   462
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   463
// You do not want to write functions like contains, first 
71
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   464
// and so on for every type of lists.
19dff7218b0d updated
Christian Urban <urbanc@in.tum.de>
parents: 68
diff changeset
   465
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   466
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   467
def length_string_list(lst: List[String]): Int = lst match {
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   468
  case Nil => 0
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   469
  case x::xs => 1 + length_string_list(xs)
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   470
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   471
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   472
def length_int_list(lst: List[Int]): Int = lst match {
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   473
  case Nil => 0
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   474
  case x::xs => 1 + length_int_list(xs)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   475
}
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   476
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   477
length_string_list(List("1", "2", "3", "4"))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   478
length_int_list(List(1, 2, 3, 4))
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   479
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   480
//-----
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   481
def length[A](lst: List[A]): Int = lst match {
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   482
  case Nil => 0
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   483
  case x::xs => 1 + length(xs)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   484
}
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   485
length(List("1", "2", "3", "4"))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   486
length(List(1, 2, 3, 4))
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   487
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   488
def map[A, B](lst: List[A], f: A => B): List[B] = lst match {
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   489
  case Nil => Nil
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   490
  case x::xs => f(x)::map_int_list(xs, f) 
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   491
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   492
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   493
map_int_list(List(1, 2, 3, 4), square)
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   494
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   495
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   496
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   497
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   498
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents: 155
diff changeset
   499
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   500
// Cool Stuff
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   501
//============
72
9d8b172f5337 updated
Christian Urban <urbanc@in.tum.de>
parents: 71
diff changeset
   502
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   503
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   504
// Implicits 
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   505
//===========
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   506
//
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   507
// For example adding your own methods to Strings:
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   508
// Imagine you want to increment strings, like
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   509
//
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   510
//     "HAL".increment
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   511
//
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   512
// you can avoid ugly fudges, like a MyString, by
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   513
// using implicit conversions.
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   514
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   515
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   516
implicit class MyString(s: String) {
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   517
  def increment = for (c <- s) yield (c + 1).toChar 
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   518
}
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   519
155
371acb50643d updated
Christian Urban <urbanc@in.tum.de>
parents: 153
diff changeset
   520
"HAL".increment
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   521
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   522
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   523
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 8
diff changeset
   524
67
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   525
ca5884c2e3bd updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   526