progs/lecture5.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Wed, 04 Nov 2020 15:35:31 +0000
changeset 351 591b9005157e
parent 333 24bc76d97db2
child 380 d19b0a50ceb9
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
238
046f37a262d0 updated
updated
parents: 226
diff changeset
     1
// Scala Lecture 5
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     2
//=================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     3
333
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
     4
// TODO: word count for a very large file (40GB or so)
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
     5
// Transform Farenheit into Celsius
24bc76d97db2 updated
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
     6
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
     7
238
046f37a262d0 updated
updated
parents: 226
diff changeset
     8
// Laziness with style
046f37a262d0 updated
updated
parents: 226
diff changeset
     9
//=====================
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    10
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
    11
// The concept of lazy evaluation doesn’t really 
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    12
// exist in non-functional languages. C-like languages
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
    13
// are (sort of) strict. To see the difference, consider
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    14
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    15
def square(x: Int) = x * x
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    16
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    17
square(42 + 8)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    18
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    19
// This is called "strict evaluation".
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    20
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
    21
// On the contrary, say we have a pretty expensive operation:
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    22
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    23
def peop(n: BigInt): Boolean = peop(n + 1) 
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
    24
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    25
val a = "foo"
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
    26
val b = "foo"
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    27
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    28
if (a == b || peop(0)) println("true") else println("false")
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    29
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    30
// This is called "lazy evaluation":
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    31
// you delay compuation until it is really 
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    32
// needed. Once calculated though, the result
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    33
// does not need to be re-calculated.
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    34
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    35
// A useful example is
328
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
    36
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    37
def time_needed[T](i: Int, code: => T) = {
046f37a262d0 updated
updated
parents: 226
diff changeset
    38
  val start = System.nanoTime()
046f37a262d0 updated
updated
parents: 226
diff changeset
    39
  for (j <- 1 to i) code
046f37a262d0 updated
updated
parents: 226
diff changeset
    40
  val end = System.nanoTime()
046f37a262d0 updated
updated
parents: 226
diff changeset
    41
  f"${(end - start) / (i * 1.0e9)}%.6f secs"
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    42
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    43
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    44
// A slightly less obvious example: Prime Numbers.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    45
// (I do not care how many) primes: 2, 3, 5, 7, 9, 11, 13 ....
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    46
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    47
def generatePrimes (s: LazyList[Int]): LazyList[Int] =
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    48
  s.head #:: generatePrimes(s.tail.filter(_ % s.head != 0))
046f37a262d0 updated
updated
parents: 226
diff changeset
    49
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    50
val primes = generatePrimes(LazyList.from(2))
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    51
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    52
// the first 10 primes
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
    53
primes.take(100).toList
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    54
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    55
time_needed(1, primes.filter(_ > 100).take(3000).toList)
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    56
time_needed(1, primes.filter(_ > 100).take(3000).toList)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    57
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    58
// A Stream (LazyList) of successive numbers:
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    59
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    60
LazyList.from(2).take(10)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    61
LazyList.from(2).take(10).force
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    62
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    63
// An Iterative version of the Fibonacci numbers
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    64
def fibIter(a: BigInt, b: BigInt): LazyList[BigInt] =
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    65
  a #:: fibIter(b, a + b)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    66
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    67
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    68
fibIter(1, 1).take(10).force
046f37a262d0 updated
updated
parents: 226
diff changeset
    69
fibIter(8, 13).take(10).force
046f37a262d0 updated
updated
parents: 226
diff changeset
    70
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    71
fibIter(1, 1).drop(10000).take(1)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    72
fibIter(1, 1).drop(10000).take(1).force
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    73
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    74
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
    75
// LazyLists are good for testing
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    76
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    77
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    78
// Regular expressions - the power of DSLs in Scala
238
046f37a262d0 updated
updated
parents: 226
diff changeset
    79
//                                     and Laziness
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    80
//==================================================
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    81
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    82
abstract class Rexp
226
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    83
case object ZERO extends Rexp                     // nothing
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    84
case object ONE extends Rexp                      // the empty string
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    85
case class CHAR(c: Char) extends Rexp             // a character c
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    86
case class ALT(r1: Rexp, r2: Rexp) extends Rexp   // alternative  r1 + r2
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    87
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp   // sequence     r1 . r2  
5e489c9fe47b updated
Christian Urban <urbanc@in.tum.de>
parents: 225
diff changeset
    88
case class STAR(r: Rexp) extends Rexp             // star         r*
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    89
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    90
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    91
// some convenience for typing in regular expressions
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    92
import scala.language.implicitConversions    
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    93
import scala.language.reflectiveCalls 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    94
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    95
def charlist2rexp(s: List[Char]): Rexp = s match {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    96
  case Nil => ONE
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    97
  case c::Nil => CHAR(c)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    98
  case c::s => SEQ(CHAR(c), charlist2rexp(s))
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
    99
}
224
42d760984496 updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   100
implicit def string2rexp(s: String): Rexp = 
42d760984496 updated
Christian Urban <urbanc@in.tum.de>
parents: 223
diff changeset
   101
  charlist2rexp(s.toList)
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   102
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   103
implicit def RexpOps (r: Rexp) = new {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   104
  def | (s: Rexp) = ALT(r, s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   105
  def % = STAR(r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   106
  def ~ (s: Rexp) = SEQ(r, s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   107
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   108
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   109
implicit def stringOps (s: String) = new {
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   110
  def | (r: Rexp) = ALT(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   111
  def | (r: String) = ALT(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   112
  def % = STAR(s)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   113
  def ~ (r: Rexp) = SEQ(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   114
  def ~ (r: String) = SEQ(s, r)
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   115
}
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   116
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   117
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   118
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   119
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   120
//example regular expressions
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   121
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   122
val sign = "+" | "-" | ""
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   123
val number = sign ~ digit ~ digit.% 
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   124
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   125
// Task: enumerate exhaustively regular expressions
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   126
// starting from small ones towards bigger ones.
046f37a262d0 updated
updated
parents: 226
diff changeset
   127
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   128
// 1st idea: enumerate them all in a Set
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   129
// up to a level
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   130
046f37a262d0 updated
updated
parents: 226
diff changeset
   131
def enuml(l: Int, s: String) : Set[Rexp] = l match {
046f37a262d0 updated
updated
parents: 226
diff changeset
   132
  case 0 => Set(ZERO, ONE) ++ s.map(CHAR).toSet
046f37a262d0 updated
updated
parents: 226
diff changeset
   133
  case n =>  
046f37a262d0 updated
updated
parents: 226
diff changeset
   134
    val rs = enuml(n - 1, s)
046f37a262d0 updated
updated
parents: 226
diff changeset
   135
    rs ++
046f37a262d0 updated
updated
parents: 226
diff changeset
   136
    (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) ++
046f37a262d0 updated
updated
parents: 226
diff changeset
   137
    (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) ++
046f37a262d0 updated
updated
parents: 226
diff changeset
   138
    (for (r1 <- rs) yield STAR(r1))
046f37a262d0 updated
updated
parents: 226
diff changeset
   139
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   140
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   141
enuml(1, "a")
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   142
enuml(1, "a").size
046f37a262d0 updated
updated
parents: 226
diff changeset
   143
enuml(2, "a").size
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   144
enuml(3, "a").size // out of heap space
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   145
046f37a262d0 updated
updated
parents: 226
diff changeset
   146
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   147
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   148
def enum(rs: LazyList[Rexp]) : LazyList[Rexp] = 
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   149
  rs #::: enum( (for (r1 <- rs; r2 <- rs) yield ALT(r1, r2)) #:::
046f37a262d0 updated
updated
parents: 226
diff changeset
   150
                (for (r1 <- rs; r2 <- rs) yield SEQ(r1, r2)) #:::
046f37a262d0 updated
updated
parents: 226
diff changeset
   151
                (for (r1 <- rs) yield STAR(r1)) )
046f37a262d0 updated
updated
parents: 226
diff changeset
   152
046f37a262d0 updated
updated
parents: 226
diff changeset
   153
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   154
enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(200).force
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   155
enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(5_000_000).force
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   156
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   157
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   158
def depth(r: Rexp) : Int = r match {
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   159
  case ZERO => 0
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   160
  case ONE => 0
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   161
  case CHAR(_) => 0
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   162
  case ALT(r1, r2) => Math.max(depth(r1), depth(r2)) + 1
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   163
  case SEQ(r1, r2) => Math.max(depth(r1), depth(r2)) + 1 
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   164
  case STAR(r1) => depth(r1) + 1
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   165
}
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   166
046f37a262d0 updated
updated
parents: 226
diff changeset
   167
046f37a262d0 updated
updated
parents: 226
diff changeset
   168
val is = 
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   169
  (enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b')))
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   170
    .dropWhile(depth(_) < 3)
046f37a262d0 updated
updated
parents: 226
diff changeset
   171
    .take(10).foreach(println))
046f37a262d0 updated
updated
parents: 226
diff changeset
   172
046f37a262d0 updated
updated
parents: 226
diff changeset
   173
328
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   174
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   175
// Polymorphic Types
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   176
//===================
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   177
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   178
// You do not want to write functions like contains, first, 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   179
// length and so on for every type of lists.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   180
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   181
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   182
def length_string_list(lst: List[String]): Int = lst match {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   183
  case Nil => 0
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   184
  case _::xs => 1 + length_string_list(xs)
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   185
}
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   186
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   187
def length_int_list(lst: List[Int]): Int = lst match {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   188
  case Nil => 0
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   189
  case x::xs => 1 + length_int_list(xs)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   190
}
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   191
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   192
length_string_list(List("1", "2", "3", "4"))
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   193
length_string_list(List(1, 2, 3, 4))
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   194
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   195
// you can make the function parametric in type(s)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   196
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   197
def length[A](lst: List[A]): Int = lst match {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   198
  case Nil => 0
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   199
  case x::xs => 1 + length(xs)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   200
}
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   201
length[String](List("1", "2", "3", "4"))
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   202
length(List(1, 2, 3, 4))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   203
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   204
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   205
def map[A, B](lst: List[A], f: A => B): List[B] = lst match {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   206
  case Nil => Nil
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   207
  case x::xs => f(x)::map(xs, f) 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   208
}
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   209
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   210
map(List(1, 2, 3, 4), (x: Int) => x.toString)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   211
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   212
046f37a262d0 updated
updated
parents: 226
diff changeset
   213
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   214
// distinct / distinctBy
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   215
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   216
val ls = List(1,2,3,3,2,4,3,2,1)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   217
ls.distinct
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   218
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   219
// .minBy(_._2)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   220
// .sortBy(_._1)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   221
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   222
def distinctBy[B, C](xs: List[B], 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   223
                     f: B => C, 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   224
                     acc: List[C] = Nil): List[B] = xs match {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   225
  case Nil => Nil
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   226
  case x::xs => {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   227
    val res = f(x)
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   228
    if (acc.contains(res) distinctBy(xs, f, acc)  
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   229
    else x::distinctBy(xs, f, res::acc)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   230
  }
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   231
} 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   232
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   233
val cs = List('A', 'b', 'a', 'c', 'B', 'D', 'd')
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   234
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   235
distinctBy(cs, (c:Char) => c.toUpper)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   236
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   237
// since 2.13
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   238
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   239
cs.distinctBy((c:Char) => c.toUpper)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   240
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   241
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   242
// Type inference is local in Scala
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   243
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   244
def id[T](x: T) : T = x
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   245
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   246
val x = id(322)          // Int
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   247
val y = id("hey")        // String
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   248
val z = id(Set(1,2,3,4)) // Set[Int]
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   249
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   250
id[+A, -B]
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   251
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   252
// The type variable concept in Scala can get really complicated.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   253
//
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   254
// - variance (OO)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   255
// - bounds (subtyping)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   256
// - quantification
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   257
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   258
// Java has issues with this too: Java allows
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   259
// to write the following incorrect code, and
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   260
// only recovers by raising an exception
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   261
// at runtime.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   262
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   263
// Object[] arr = new Integer[10];
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   264
// arr[0] = "Hello World";
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   265
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   266
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   267
// Scala gives you a compile-time error, which
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   268
// is much better.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   269
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   270
var arr = Array[Int]()
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   271
arr(0) = "Hello World"
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   272
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   273
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   274
// (Immutable)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   275
// Object Oriented Programming in Scala
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   276
//
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   277
// =====================================
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   278
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   279
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   280
abstract class Animal 
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   281
case class Bird(name: String) extends Animal {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   282
   override def toString = name
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   283
}
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   284
case class Mammal(name: String) extends Animal
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   285
case class Reptile(name: String) extends Animal
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   286
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   287
Mammal("Zebra")
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   288
println(Mammal("Zebra"))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   289
println(Mammal("Zebra").toString)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   290
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   291
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   292
Bird("Sparrow")
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   293
println(Bird("Sparrow"))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   294
println(Bird("Sparrow").toString)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   295
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   296
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   297
// There is a very convenient short-hand notation
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   298
// for constructors:
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   299
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   300
class Fraction(x: Int, y: Int) {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   301
  def numer = x
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   302
  def denom = y
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   303
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   304
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   305
val half = new Fraction(1, 2)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   306
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   307
case class Fraction(numer: Int, denom: Int)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   308
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   309
val half = Fraction(1, 2)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   310
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   311
half.denom
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   312
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   313
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   314
// In mandelbrot.scala I used complex (imaginary) numbers 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   315
// and implemented the usual arithmetic operations for complex 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   316
// numbers.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   317
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   318
case class Complex(re: Double, im: Double) { 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   319
  // represents the complex number re + im * i
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   320
  def +(that: Complex) = Complex(this.re + that.re, this.im + that.im)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   321
  def -(that: Complex) = Complex(this.re - that.re, this.im - that.im)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   322
  def *(that: Complex) = Complex(this.re * that.re - this.im * that.im,
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   323
                                 this.re * that.im + that.re * this.im)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   324
  def *(that: Double) = Complex(this.re * that, this.im * that)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   325
  def abs = Math.sqrt(this.re * this.re + this.im * this.im)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   326
}
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   327
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   328
val test = Complex(1, 2) + Complex (3, 4)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   329
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   330
// this could have equally been written as
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   331
val test = Complex(1, 2).+(Complex (3, 4))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   332
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   333
// this applies to all methods, but requires
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   334
import scala.language.postfixOps
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   335
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   336
List(5, 2, 3, 4).sorted
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   337
List(5, 2, 3, 4) sorted
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   338
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   339
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   340
// ...to allow the notation n + m * i
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   341
import scala.language.implicitConversions   
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   342
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   343
val i = Complex(0, 1)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   344
implicit def double2complex(re: Double) = Complex(re, 0)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   345
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   346
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   347
val inum1 = -2.0 + -1.5 * i
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   348
val inum2 =  1.0 +  1.5 * i
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   349
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   350
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   351
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   352
// All is public by default....so no public is needed.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   353
// You can have the usual restrictions about private 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   354
// values and methods, if you are MUTABLE !!!
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   355
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   356
case class BankAccount(init: Int) {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   357
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   358
  private var balance = init
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   359
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   360
  def deposit(amount: Int): Unit = {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   361
    if (amount > 0) balance = balance + amount
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   362
  }
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   363
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   364
  def withdraw(amount: Int): Int =
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   365
    if (0 < amount && amount <= balance) {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   366
      balance = balance - amount
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   367
      balance
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   368
    } else throw new Error("insufficient funds")
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   369
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   370
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   371
// BUT since we are completely IMMUTABLE, this is 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   372
// virtually of not concern to us.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   373
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   374
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   375
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   376
// another example about Fractions
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   377
import scala.language.implicitConversions
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   378
import scala.language.reflectiveCalls
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   379
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   380
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   381
case class Fraction(numer: Int, denom: Int) {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   382
  override def toString = numer.toString + "/" + denom.toString
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   383
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   384
  def +(other: Fraction) = Fraction(numer + other.numer, denom + other.denom)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   385
  def /(other: Fraction) = Fraction(numer * other.denom, denom * other.numer)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   386
 }
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   387
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   388
implicit def Int2Fraction(x: Int) = Fraction(x, 1)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   389
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   390
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   391
val half = Fraction(1, 2)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   392
val third = Fraction (1, 3)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   393
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   394
half + third
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   395
half / third
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   396
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   397
(1 / 3) + half
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   398
(1 / 2) + third
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   399
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   400
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   401
// DFAs in Scala  
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   402
//===============
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   403
import scala.util.Try
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   404
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   405
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   406
// A is the state type
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   407
// C is the input (usually characters)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   408
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   409
case class DFA[A, C](start: A,              // starting state
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   410
                     delta: (A, C) => A,    // transition function
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   411
                     fins:  A => Boolean) { // final states (Set)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   412
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   413
  def deltas(q: A, s: List[C]) : A = s match {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   414
    case Nil => q
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   415
    case c::cs => deltas(delta(q, c), cs)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   416
  }
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   417
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   418
  def accepts(s: List[C]) : Boolean = 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   419
    Try(fins(deltas(start, s))) getOrElse false
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   420
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   421
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   422
// the example shown in the handout 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   423
abstract class State
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   424
case object Q0 extends State
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   425
case object Q1 extends State
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   426
case object Q2 extends State
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   427
case object Q3 extends State
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   428
case object Q4 extends State
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   429
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   430
val delta : (State, Char) => State = 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   431
  { case (Q0, 'a') => Q1
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   432
    case (Q0, 'b') => Q2
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   433
    case (Q1, 'a') => Q4
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   434
    case (Q1, 'b') => Q2
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   435
    case (Q2, 'a') => Q3
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   436
    case (Q2, 'b') => Q2
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   437
    case (Q3, 'a') => Q4
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   438
    case (Q3, 'b') => Q0
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   439
    case (Q4, 'a') => Q4
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   440
    case (Q4, 'b') => Q4 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   441
    case _ => throw new Exception("Undefined") }
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   442
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   443
val dfa = DFA(Q0, delta, Set[State](Q4))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   444
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   445
dfa.accepts("abaaa".toList)     // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   446
dfa.accepts("bbabaab".toList)   // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   447
dfa.accepts("baba".toList)      // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   448
dfa.accepts("abc".toList)       // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   449
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   450
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   451
// NFAs (Nondeterministic Finite Automata)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   452
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   453
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   454
case class NFA[A, C](starts: Set[A],          // starting states
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   455
                     delta: (A, C) => Set[A], // transition function
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   456
                     fins:  A => Boolean) {   // final states 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   457
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   458
  // given a state and a character, what is the set of 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   459
  // next states? if there is none => empty set
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   460
  def next(q: A, c: C) : Set[A] = 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   461
    Try(delta(q, c)) getOrElse Set[A]() 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   462
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   463
  def nexts(qs: Set[A], c: C) : Set[A] =
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   464
    qs.flatMap(next(_, c))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   465
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   466
  // depth-first version of accepts
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   467
  def search(q: A, s: List[C]) : Boolean = s match {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   468
    case Nil => fins(q)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   469
    case c::cs => next(q, c).exists(search(_, cs))
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   470
  }
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   471
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   472
  def accepts(s: List[C]) : Boolean =
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   473
    starts.exists(search(_, s))
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   474
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   475
046f37a262d0 updated
updated
parents: 226
diff changeset
   476
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   477
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   478
// NFA examples
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   479
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   480
val nfa_trans1 : (State, Char) => Set[State] = 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   481
  { case (Q0, 'a') => Set(Q0, Q1) 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   482
    case (Q0, 'b') => Set(Q2) 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   483
    case (Q1, 'a') => Set(Q1) 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   484
    case (Q2, 'b') => Set(Q2) }
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   485
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   486
val nfa = NFA(Set[State](Q0), nfa_trans1, Set[State](Q2))
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   487
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   488
nfa.accepts("aa".toList)             // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   489
nfa.accepts("aaaaa".toList)          // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   490
nfa.accepts("aaaaab".toList)         // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   491
nfa.accepts("aaaaabbb".toList)       // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   492
nfa.accepts("aaaaabbbaaa".toList)    // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   493
nfa.accepts("ac".toList)             // false
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   494
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   495
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   496
// Q: Why the kerfuffle about the polymorphic types in DFAs/NFAs?
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   497
// A: Subset construction. Here the state type for the DFA is
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   498
//    sets of states.
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   499
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   500
def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   501
  DFA(nfa.starts, 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   502
      { case (qs, c) => nfa.nexts(qs, c) }, 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   503
      _.exists(nfa.fins))
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   504
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   505
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   506
subset(nfa).accepts("aa".toList)             // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   507
subset(nfa).accepts("aaaaa".toList)          // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   508
subset(nfa).accepts("aaaaab".toList)         // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   509
subset(nfa).accepts("aaaaabbb".toList)       // true
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   510
subset(nfa).accepts("aaaaabbbaaa".toList)    // false
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   511
subset(nfa).accepts("ac".toList)             // false
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   512
046f37a262d0 updated
updated
parents: 226
diff changeset
   513
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   514
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   515
// The End ... Almost Christmas
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   516
//===============================
046f37a262d0 updated
updated
parents: 226
diff changeset
   517
046f37a262d0 updated
updated
parents: 226
diff changeset
   518
// I hope you had fun!
046f37a262d0 updated
updated
parents: 226
diff changeset
   519
046f37a262d0 updated
updated
parents: 226
diff changeset
   520
// A function should do one thing, and only one thing.
046f37a262d0 updated
updated
parents: 226
diff changeset
   521
046f37a262d0 updated
updated
parents: 226
diff changeset
   522
// Make your variables immutable, unless there's a good 
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   523
// reason not to. Usually there is not.
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   524
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   525
// I did it once, but this is actually not a good reason:
240
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   526
// generating new labels:
b8cdaf51ffef updated
Christian Urban <urbanc@in.tum.de>
parents: 238
diff changeset
   527
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   528
var counter = -1
222
e52cc402caee updated
Christian Urban <urbanc@in.tum.de>
parents: 218
diff changeset
   529
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   530
def Fresh(x: String) = {
046f37a262d0 updated
updated
parents: 226
diff changeset
   531
  counter += 1
046f37a262d0 updated
updated
parents: 226
diff changeset
   532
  x ++ "_" ++ counter.toString()
046f37a262d0 updated
updated
parents: 226
diff changeset
   533
}
046f37a262d0 updated
updated
parents: 226
diff changeset
   534
046f37a262d0 updated
updated
parents: 226
diff changeset
   535
Fresh("x")
046f37a262d0 updated
updated
parents: 226
diff changeset
   536
Fresh("x")
046f37a262d0 updated
updated
parents: 226
diff changeset
   537
046f37a262d0 updated
updated
parents: 226
diff changeset
   538
046f37a262d0 updated
updated
parents: 226
diff changeset
   539
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   540
// I think you can be productive on Day 1, but the 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   541
// language is deep.
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   542
//
046f37a262d0 updated
updated
parents: 226
diff changeset
   543
// http://scalapuzzlers.com
046f37a262d0 updated
updated
parents: 226
diff changeset
   544
//
046f37a262d0 updated
updated
parents: 226
diff changeset
   545
// http://www.latkin.org/blog/2017/05/02/when-the-scala-compiler-doesnt-help/
046f37a262d0 updated
updated
parents: 226
diff changeset
   546
328
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   547
val two   = 0.2
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   548
val one   = 0.1
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   549
val eight = 0.8
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   550
val six   = 0.6
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   551
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   552
two - one == one
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   553
eight - six == two
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   554
eight - six
328
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   555
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   556
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   557
// problems about equality and type-errors
328
0e591f806290 updated
Christian Urban <urbanc@in.tum.de>
parents: 326
diff changeset
   558
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   559
List(1, 2, 3).contains("your cup")   // should not compile, but retruns false
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   560
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   561
List(1, 2, 3) == Vector(1, 2, 3)     // again should not compile, but returns true
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   562
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   563
046f37a262d0 updated
updated
parents: 226
diff changeset
   564
// I like best about Scala that it lets me often write
046f37a262d0 updated
updated
parents: 226
diff changeset
   565
// concise, readable code. And it hooks up with the 
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   566
// Isabelle theorem prover. 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   567
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   568
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   569
// Puzzlers
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   570
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   571
val month = 12
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   572
val day = 24
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   573
val (hour, min, sec) = (12, 0, 0)
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   574
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   575
// use lowercase names for variable 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   576
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   577
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   578
//==================
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   579
val oneTwo = Seq(1, 2, 3).permutations
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   580
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   581
if (oneTwo.length > 0) {
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   582
  println("Permutations of 1,2 and 3:")
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   583
  oneTwo.foreach(println)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   584
}
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   585
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   586
val threeFour = Seq(3, 4, 5).permutations
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   587
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   588
if (!threeFour.isEmpty) {
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   589
  println("Permutations of 3, 4 and 5:")
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   590
  threeFour.foreach(println)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   591
}
238
046f37a262d0 updated
updated
parents: 226
diff changeset
   592
326
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   593
//==================
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   594
val (a, b, c) =
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   595
    if (4 < 5) {
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   596
        "bar"
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   597
    } else { 
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   598
        Some(10)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   599
    }
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   600
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   601
//Because when an expression has multiple return branches, Scala tries to
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   602
//be helpful, by picking the first common ancestor type of all the
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   603
//branches as the type of the whole expression.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   604
//
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   605
//In this case, one branch has type String and the other has type
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   606
//Option[Int], so the compiler decides that what the developer really
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   607
//wants is for the whole if/else expression to have type Serializable,
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   608
//since that’s the most specific type to claim both String and Option as
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   609
//descendants.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   610
//
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   611
//And guess what, Tuple3[A, B, C] is also Serializable, so as far as the
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   612
//compiler is concerned, the assignment of the whole mess to (a, b, c)
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   613
//can’t be proven invalid. So it gets through with a warning,
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   614
//destined to fail at runtime.
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   615
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   616
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   617
//================
e5453add7df6 updated
Christian Urban <urbanc@in.tum.de>
parents: 247
diff changeset
   618
// does not work anymore in 2.13.0
329
8a34b2ebc8cc updated
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   619
val numbers = List("1", "2").toSet + "3"