equal
deleted
inserted
replaced
1 // Scala Lecture 3 |
1 // Scala Lecture 3 |
2 //================= |
2 //================= |
3 |
3 |
|
4 // - Higher-Order functions |
4 // - maps (behind for-comprehensions) |
5 // - maps (behind for-comprehensions) |
|
6 |
5 // - Pattern-Matching |
7 // - Pattern-Matching |
|
8 |
|
9 def fib(n: Int) : Int = n match { |
|
10 case 0 => 1 |
|
11 case 1 => 1 |
|
12 case n => fib(n - 1) + fib(n - 2) |
|
13 } |
|
14 |
6 |
15 |
7 abstract class Rexp |
16 abstract class Rexp |
8 case object ZERO extends Rexp // matches nothing |
17 case object ZERO extends Rexp // matches nothing |
9 case object ONE extends Rexp // matches the empty string |
18 case object ONE extends Rexp // matches the empty string |
10 case class CHAR(c: Char) extends Rexp // matches a character c |
19 case class CHAR(c: Char) extends Rexp // matches a character c |
308 |
317 |
309 |
318 |
310 // Sudoku |
319 // Sudoku |
311 //======== |
320 //======== |
312 |
321 |
|
322 // uses Strings for games |
313 |
323 |
314 type Pos = (Int, Int) |
324 type Pos = (Int, Int) |
315 val emptyValue = '.' |
325 val emptyValue = '.' |
316 val maxValue = 9 |
326 val maxValue = 9 |
317 |
327 |
347 allValues.diff(toAvoid(game, pos)) |
357 allValues.diff(toAvoid(game, pos)) |
348 |
358 |
349 def search(game: String): List[String] = { |
359 def search(game: String): List[String] = { |
350 if (isDone(game)) List(game) |
360 if (isDone(game)) List(game) |
351 else |
361 else |
352 candidates(game, emptyPosition(game)).par. |
362 candidates(game, emptyPosition(game)). |
353 map(c => search(update(game, empty(game), c))).toList.flatten |
363 map(c => search(update(game, empty(game), c))).flatten |
354 } |
364 } |
355 |
365 |
356 |
366 |
357 def search1T(games: List[String]): Option[String] = games match { |
367 def search1T(games: List[String]): Option[String] = games match { |
358 case Nil => None |
368 case Nil => None |