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