progs/lecture2.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 17 Nov 2016 11:17:04 +0000
changeset 57 9b88f9e04344
parent 56 6fa91be92d0c
child 58 93a2b6e4b84c
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     1
// Scala Lecture 2
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     2
//=================
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     3
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     4
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     5
// Option type
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
     6
//=============
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
     7
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
     8
//in Java if something unusually happens, you return null;
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
     9
//in Scala you use Option
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    10
//   - if the value is present, you use Some(value)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    11
//   - if no value is present, you use None
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    12
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    13
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    14
List(7,2,3,4,5,6).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    15
List(5,6,7,8,9).find(_ < 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    16
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    17
val lst = List(None, Some(1), Some(2), None, Some(3))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    18
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    19
lst.flatten
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    20
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    21
Some(1).get
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    22
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    23
Some(1).isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    24
None.isDefined
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    25
51
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    26
val ps = List((3, 0), (3, 2), (4, 2), (2, 0), (1, 0), (1, 1))
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    27
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    28
for ((x, y) <- ps) yield {
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    29
  if (y == 0) None else Some(x / y)
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    30
}
0e60e6c24b99 updated
Christian Urban <urbanc@in.tum.de>
parents: 39
diff changeset
    31
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    32
// getOrElse is for setting a default value
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    33
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    34
val lst = List(None, Some(1), Some(2), None, Some(3))
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    35
for (x <- lst) yield x.getOrElse(0)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    36
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    37
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    38
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    39
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    40
// error handling with option (no exceptions)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    41
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    42
//  Try(something).getOrElse(what_to_do_in_an_exception)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    43
//
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    44
import scala.util._
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    45
import io.Source
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    46
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    47
Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    48
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    49
Try(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString).getOrElse("")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    50
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    51
Try(Some(Source.fromURL("""http://www.inf.kcl.ac.uk/staff/urbanc/""").mkString)).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    52
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    53
// a function that turns strings into numbers
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    54
Integer.parseInt("12u34")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    55
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    56
def get_me_an_int(s: String): Option[Int] = 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    57
 Try(Some(Integer.parseInt(s))).getOrElse(None)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    58
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    59
val lst = List("12345", "foo", "5432", "bar", "x21")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    60
for (x <- lst) yield get_me_an_int(x)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    61
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    62
// summing all the numbers
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    63
val sum = lst.flatMap(get_me_an_int(_)).sum
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    64
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    65
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    66
// This may not look any better than working with null in Java, but to
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    67
// see the value, you have to put yourself in the shoes of the
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    68
// consumer of the get_me_an_int function, and imagine you didn't
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    69
// write that function.
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    70
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    71
// In Java, if you didn't write this function, you'd have to depend on
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    72
// the Javadoc of the get_me_an_int. If you didn't look at the Javadoc, 
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    73
// you might not know that get_me_an_int could return a null, and your 
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    74
// code could potentially throw a NullPointerException.
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    75
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    76
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    77
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    78
// Type abbreviations
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    79
//====================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    80
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    81
// some syntactic convenience
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    82
type Pos = (int, Int)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    83
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    84
type Board = List[List[Int]]
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    85
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    86
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
    87
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    88
// Implicits
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    89
//===========
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    90
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    91
// for example adding your own methods to Strings:
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    92
// imagine you want to increment strings, like
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    93
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    94
//     "HAL".increment
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    95
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    96
// you can avoid ugly fudges, like a MyString, by
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    97
// using implicit conversions
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    98
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
    99
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   100
implicit class MyString(s: String) {
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   101
  def increment = for (c <- s) yield (c + 1).toChar 
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   102
}
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   103
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   104
"HAL".increment
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   105
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   106
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   107
// No return in Scala
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   108
//====================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   109
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   110
//You should not use "return" in Scala:
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   111
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   112
// A return expression, when evaluated, abandons the 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   113
// current computation and returns to the caller of the 
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   114
// function in which return appears."
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   115
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   116
def sq1(x: Int): Int = x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   117
def sq2(x: Int): Int = return x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   118
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   119
def sumq(ls: List[Int]): Int = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   120
  (for (x <- ls) yield (return x * x)).sum[Int]
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   121
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   122
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   123
sumq(List(1,2,3,4))
36
f5ed0fef41b3 updated
Christian Urban <urbanc@in.tum.de>
parents: 13
diff changeset
   124
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   125
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   126
// last expression in a function is the return statement
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   127
def square(x: Int): Int = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   128
  println(s"The argument is ${x}.")
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   129
  x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   130
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   131
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   132
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   133
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   134
// Pattern Matching
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   135
//==================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   136
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   137
// A powerful tool which is supposed to come to Java in a few years
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   138
// time (https://www.youtube.com/watch?v=oGll155-vuQ)...Scala already
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   139
// has it for many years ;o)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   140
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   141
// The general schema:
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   142
//
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   143
//    expression match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   144
//       case pattern1 => expression1
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   145
//       case pattern2 => expression2
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   146
//       ...
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   147
//       case patternN => expressionN
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   148
//    }
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   149
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   150
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   151
// remember
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   152
val lst = List(None, Some(1), Some(2), None, Some(3)).flatten
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   153
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   154
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   155
def my_flatten(xs: List[Option[Int]]): List[Int] = {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   156
  ...
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   157
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   158
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   159
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   160
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   161
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   162
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   163
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   164
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   165
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   166
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   167
def my_flatten(lst: List[Option[Int]]): List[Int] = lst match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   168
  case Nil => Nil
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   169
  case None::xs => my_flatten(xs)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   170
  case Some(n)::xs => n::my_flatten(xs)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   171
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   172
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   173
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   174
// another example
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   175
def get_me_a_string(n: Int): String = n match {
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   176
  case 0 => "zero"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   177
  case 1 => "one"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   178
  case 2 => "two"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   179
  case _ => "many"
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   180
}
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   181
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   182
get_me_a_string(0)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   183
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   184
// User-defined Datatypes
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   185
//========================
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   186
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   187
abstract class Tree
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   188
case class Node(elem: Int, left: Tree, right: Tree) extends Tree
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   189
case class Leaf() extends Tree
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   190
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   191
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   192
def insert(tr: Tree, n: Int): Tree = tr match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   193
  case Leaf() => Node(n, Leaf(), Leaf())
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   194
  case Node(m, left, right) => 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   195
    if (n == m) Node(m, left, right) 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   196
    else if (n < m) Node(m, insert(left, n), right)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   197
    else Node(m, left, insert(right, n))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   198
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   199
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   200
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   201
val t1 = Node(4, Node(2, Leaf(), Leaf()), Node(7, Leaf(), Leaf()))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   202
insert(t1, 3)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   203
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   204
def balance(tr: Tree): Int = tr match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   205
  case Leaf() => 0
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   206
  case Node(_, left, right) => balance(left) - balance(right)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   207
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   208
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   209
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   210
// another example
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   211
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   212
abstract class Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   213
case class King() extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   214
case class Peer(deg: String, terr: String, succ: Int) extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   215
case class Knight(name: String) extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   216
case class Peasant(name: String) extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   217
case class Clown() extends Person
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   218
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   219
def title(p: Person): String = p match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   220
  case King() => "His Majesty the King"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   221
  case Peer(deg, terr, _) => s"The ${deg} of ${terr}"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   222
  case Knight(name) => s"Sir ${name}"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   223
  case Peasant(name) => name
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   224
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   225
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   226
def superior(p1: Person, p2: Person): Boolean = (p1, p2) match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   227
  case (King(), _) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   228
  case (Peer(_,_,_), Knight(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   229
  case (Peer(_,_,_), Peasant(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   230
  case (Peer(_,_,_), Clown()) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   231
  case (Knight(_), Peasant(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   232
  case (Knight(_), Clown()) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   233
  case (Clown(), Peasant(_)) => true
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   234
  case _ => false
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   235
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   236
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   237
val people = List(Knight("David"), 
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   238
                  Peer("Duke", "Norfolk", 84), 
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   239
                  Peasant("Christian"), 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   240
                  King(), 
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   241
                  Clown())
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   242
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   243
println(people.sortWith(superior(_, _)).mkString(", "))
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   244
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   245
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   246
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   247
// Higher-Order Functions
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   248
//========================
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   249
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   250
// functions can take functions as arguments
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   251
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   252
val lst = (1 to 10).toList
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   253
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   254
def even(x: Int): Boolean = x % 2 == 0
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   255
def odd(x: Int): Boolean = x % 2 == 1
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   256
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   257
lst.filter(x => even(x))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   258
lst.filter(even(_))
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   259
lst.filter(even)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   260
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   261
lst.find(_ > 8)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   262
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   263
def square(x: Int): Int = x * x
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   264
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   265
lst.map(square)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   266
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   267
lst.map(square).filter(_ > 4)
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   268
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   269
lst.map(square).filter(_ > 4).map(square)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   270
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   271
// in my collatz.scala
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   272
//(1 to bnd).map(i => (collatz(i), i)).maxBy(_._1)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   273
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   274
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   275
// type of functions, for example f: Int => Int
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   276
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   277
def my_map_int(lst: List[Int], f: Int => Int): List[Int] = lst match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   278
  case Nil => Nil
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   279
  case x::xs => f(x)::my_map_int(xs, f)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   280
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   281
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   282
my_map_int(lst, square)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   283
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   284
// other function types
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   285
//
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   286
// f1: (Int, Int) => Int
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   287
// f2: List[String] => Option[Int]
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   288
// ... 
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   289
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   290
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   291
def sumOf(f: Int => Int, lst: List[Int]): Int = lst match {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   292
  case Nil => 0
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   293
  case x::xs => f(x) + sumOf(f, xs)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   294
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   295
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   296
def sum_squares(lst: List[Int]) = sumOf(square, lst)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   297
def sum_cubes(lst: List[Int])   = sumOf(x => x * x * x, lst)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   298
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   299
sum_squares(lst)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   300
sum_cubes(lst)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   301
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   302
def fact(n: Int): Int = 
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   303
  if (n == 0) 1 else n * fact(n - 1)
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   304
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   305
def sum_fact(lst: List[Int]) = sumOf(fact, lst)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   306
sum_fact(lst)
56
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   307
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   308
// Avoid being mutable
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   309
//=====================
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   310
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   311
// a student showed me...
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   312
import scala.collection.mutable.ListBuffer
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   313
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   314
def collatz_max(bnd: Long): (Long, Long) = {
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   315
  val colNos = ListBuffer[(Long, Long)]()
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   316
  for (i <- (1L to bnd).toList) colNos += ((collatz(i), i))
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   317
  colNos.max
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   318
}
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   319
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   320
def collatz_max(bnd: Long): (Long, Long) = {
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   321
  (1L to bnd).map((i) => (collatz(i), i)).maxBy(_._1)
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   322
}
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   323
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   324
//views -> lazy collection
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   325
def collatz_max(bnd: Long): (Long, Long) = {
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   326
  (1L to bnd).view.map((i) => (collatz(i), i)).maxBy(_._1)
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   327
}
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   328
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   329
// raises a GC exception
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   330
(1 to 1000000000).filter(_ % 2 == 0).take(10).toList
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   331
// ==> java.lang.OutOfMemoryError: GC overhead limit exceeded
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   332
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   333
(1 to 1000000000).view.filter(_ % 2 == 0).take(10).toList
6fa91be92d0c updated
Christian Urban <urbanc@in.tum.de>
parents: 55
diff changeset
   334
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   335
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   336
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   337
// Sudoku
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   338
//========
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   339
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   340
// THE POINT OF THIS CODE IS NOT TO BE SUPER
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   341
// EFFICIENT AND FAST, just explaining exhaustive
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   342
// depth-first search
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   343
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   344
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   345
val game0 = """.14.6.3..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   346
              |62...4..9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   347
              |.8..5.6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   348
              |.6.2....3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   349
              |.7..1..5.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   350
              |5....9.6.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   351
              |..6.2..3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   352
              |1..5...92
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   353
              |..7.9.41.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   354
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   355
type Pos = (Int, Int)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   356
val EmptyValue = '.'
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   357
val MaxValue = 9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   358
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   359
val allValues = "123456789".toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   360
val indexes = (0 to 8).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   361
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   362
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   363
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   364
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   365
def empty(game: String) = game.indexOf(EmptyValue)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   366
def isDone(game: String) = empty(game) == -1 
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   367
def emptyPosition(game: String) = (empty(game) % MaxValue, empty(game) / MaxValue)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   368
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   369
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   370
def get_row(game: String, y: Int) = indexes.map(col => game(y * MaxValue + col))
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   371
def get_col(game: String, x: Int) = indexes.map(row => game(x + row * MaxValue))
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   372
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   373
def get_box(game: String, pos: Pos): List[Char] = {
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   374
    def base(p: Int): Int = (p / 3) * 3
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   375
    val x0 = base(pos._1)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   376
    val y0 = base(pos._2)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   377
    val ys = (y0 until y0 + 3).toList
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   378
    (x0 until x0 + 3).toList.flatMap(x => ys.map(y => game(x + y * MaxValue)))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   379
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   380
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   381
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   382
//get_row(game0, 0)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   383
//get_row(game0, 1)
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   384
//get_box(game0, (3,1))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   385
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   386
def update(game: String, pos: Int, value: Char): String = game.updated(pos, value)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   387
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   388
def toAvoid(game: String, pos: Pos): List[Char] = 
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   389
  (get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos))
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   390
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   391
def candidates(game: String, pos: Pos): List[Char] = allValues diff toAvoid(game,pos)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   392
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   393
//candidates(game0, (0,0))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   394
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   395
def pretty(game: String): String = "\n" + (game sliding (MaxValue, MaxValue) mkString "\n")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   396
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   397
def search(game: String): List[String] = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   398
  if (isDone(game)) List(game)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   399
  else 
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   400
    candidates(game, emptyPosition(game)).map(c => search(update(game, empty(game), c))).toList.flatten
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   401
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   402
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   403
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   404
val game1 = """23.915...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   405
              |...2..54.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   406
              |6.7......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   407
              |..1.....9
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   408
              |89.5.3.17
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   409
              |5.....6..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   410
              |......9.5
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   411
              |.16..7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   412
              |...329..1""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   413
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   414
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   415
// game that is in the hard category
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   416
val game2 = """8........
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   417
              |..36.....
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   418
              |.7..9.2..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   419
              |.5...7...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   420
              |....457..
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   421
              |...1...3.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   422
              |..1....68
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   423
              |..85...1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   424
              |.9....4..""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   425
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   426
// game with multiple solutions
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   427
val game3 = """.8...9743
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   428
              |.5...8.1.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   429
              |.1.......
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   430
              |8....5...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   431
              |...8.4...
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   432
              |...3....6
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   433
              |.......7.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   434
              |.3.5...8.
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   435
              |9724...5.""".stripMargin.replaceAll("\\n", "")
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   436
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   437
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   438
search(game0).map(pretty)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   439
search(game1).map(pretty)
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   440
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   441
// for measuring time
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   442
def time_needed[T](i: Int, code: => T) = {
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   443
  val start = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   444
  for (j <- 1 to i) code
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   445
  val end = System.nanoTime()
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   446
  ((end - start) / i / 1.0e9) + " secs"
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   447
}
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   448
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   449
search(game2).map(pretty)
57
9b88f9e04344 updated
Christian Urban <urbanc@in.tum.de>
parents: 56
diff changeset
   450
search(game3).distinct.length
55
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   451
time_needed(3, search(game2))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   452
time_needed(3, search(game3))
6610c1dfa8a9 updated
Christian Urban <urbanc@in.tum.de>
parents: 53
diff changeset
   453
53
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   454
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   455
9f8751912560 updated
Christian Urban <urbanc@in.tum.de>
parents: 51
diff changeset
   456
39
c6fe374a5fca updated
Christian Urban <urbanc@in.tum.de>
parents: 37
diff changeset
   457