progs/lecture1.scala
changeset 189 ff815ca0bbcf
parent 170 37b1bfcdba79
child 195 fc3ac7b70a06
equal deleted inserted replaced
188:937c995b047a 189:ff815ca0bbcf
    49 lst(0)
    49 lst(0)
    50 lst(2)
    50 lst(2)
    51 
    51 
    52 // some alterative syntax for lists
    52 // some alterative syntax for lists
    53 
    53 
    54 1::2::3::Nil
    54 1 :: 2 :: 3 :: Nil
    55 List(1, 2, 3) ::: List(4, 5, 6)
    55 List(1, 2, 3) ::: List(4, 5, 6)
    56 
    56 
    57 // Printing/Strings
    57 // Printing/Strings
    58 //==================
    58 //==================
    59 
    59 
   124 
   124 
   125 /* in Java
   125 /* in Java
   126 val lyrics = "Sun dips down, the day has gone. \n" +
   126 val lyrics = "Sun dips down, the day has gone. \n" +
   127              "Witches, wolves and giants yawn. \n" +
   127              "Witches, wolves and giants yawn. \n" +
   128              "Queen and dragon, troll and gnome: \n" +
   128              "Queen and dragon, troll and gnome: \n" +
   129              "tired buddies head for home"
   129              "tired buddies head for home."
   130 */ 
   130 */ 
   131 
   131 
   132 val lyrics = """Sun dips down, the day has gone.
   132 val lyrics = """Sun dips down, the day has gone.
   133                 |Witches, wolves and giants yawn.
   133                 |Witches, wolves and giants yawn.
   134                 |Queen and dragon, troll and gnome:
   134                 |Queen and dragon, troll and gnome:
   135                 |tired buddies head for home""".stripMargin
   135                 |tired buddies head for home.""".stripMargin
   136 
   136 
   137 println(lyrics)
   137 println(lyrics)
   138 
   138 
   139 
   139 
   140 // Pairs/Tuples
   140 // Pairs/Tuples
   168 
   168 
   169 
   169 
   170 
   170 
   171 // If-Conditionals
   171 // If-Conditionals
   172 //=================
   172 //=================
       
   173 
       
   174 // Scala does not have a then-keyword
       
   175 // both if-else branches need to be presents
   173 
   176 
   174 def fact(n: Int) : Int = 
   177 def fact(n: Int) : Int = 
   175   if (n == 0) 1 else n * fact(n - 1)
   178   if (n == 0) 1 else n * fact(n - 1)
   176 
   179 
   177 
   180 
   184    !      not
   187    !      not
   185    && ||  and, or
   188    && ||  and, or
   186 */
   189 */
   187 
   190 
   188 
   191 
   189 def fact2(n: BigInt): BigInt = 
   192 def fact2(n: BigInt) : BigInt = 
   190   if (n == 0) 1 else n * fact2(n - 1)
   193   if (n == 0) 1 else n * fact2(n - 1)
   191 
   194 
   192 fact2(150)
   195 fact2(150)
   193 
   196 
   194 
   197 
   195 def fib(n: Int): Int =
   198 def fib(n: Int) : Int =
   196   if (n == 0) 1 else
   199   if (n == 0) 1 else
   197     if (n == 1) 1 else fib(n - 1) + fib(n - 2)
   200     if (n == 1) 1 else fib(n - 1) + fib(n - 2)
   198 
   201 
   199 
   202 
   200 //gcd - Euclid's algorithm
   203 //gcd - Euclid's algorithm
   250   for (n <- (1 to 10).toList; 
   253   for (n <- (1 to 10).toList; 
   251        m <- (1 to 10).toList) yield m * n
   254        m <- (1 to 10).toList) yield m * n
   252 
   255 
   253 mult_table.sliding(10,10).mkString("\n")
   256 mult_table.sliding(10,10).mkString("\n")
   254 
   257 
       
   258 // the list can also be constructed in any other way
       
   259 for (n <- List(10,12,4,5,7,8,10)) yield n * n
       
   260 
   255 
   261 
   256 // with if-predicates
   262 // with if-predicates
   257 
   263 
   258 for (n <- (1 to 3).toList; 
   264 for (n <- (1 to 3).toList; 
   259      m <- (1 to 3).toList;
   265      m <- (1 to 3).toList;
   268 for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n 
   274 for ((m, n) <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield m + n 
   269 
   275 
   270 for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2 
   276 for (p <- List((1, 4), (2, 3), (3, 2), (4, 1))) yield p._1 + p._2 
   271 
   277 
   272 
   278 
       
   279 // general pattern
       
   280 
       
   281 for (x <- ...) yield {
       
   282   // potentially complicated
       
   283   // calculation of a result
       
   284 }
       
   285 
       
   286 
   273 
   287 
   274 // with only a side-effect (no list is produced),
   288 // with only a side-effect (no list is produced),
   275 // has no "yield"
   289 // has no "yield"
   276 
   290 
   277 for (n <- (1 to 10)) println(n)
   291 for (n <- (1 to 10)) println(n)
   278 
       
   279 
       
   280 
       
   281 
   292 
   282 
   293 
   283 
   294 
   284 // concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12)
   295 // concurrency (ONLY WORKS IN SCALA 2.11.8, not in SCALA 2.12)
   285 //             (would need to have this wrapped into a function, or
   296 //             (would need to have this wrapped into a function, or
   301 time_needed(10, for (n <- list) yield n + 42)
   312 time_needed(10, for (n <- list) yield n + 42)
   302 time_needed(10, for (n <- list.par) yield n + 42)
   313 time_needed(10, for (n <- list.par) yield n + 42)
   303 
   314 
   304 
   315 
   305 // mutable vs immutable factorial
   316 // mutable vs immutable factorial
   306 def fact_mu(n: Long): Long = {
   317 def fact_mu(n: Long) : Long = {
   307   var result : Long = 1
   318   var result : Long = 1
   308   var i = 1
   319   var i = 1
   309   while (i <= n) {
   320   while (i <= n) {
   310     result = result * i
   321     result = result * i
   311     i = i + 1
   322     i = i + 1
   324     val l2 = for (n <- (List.fill(100000)(20 to 21)).flatten.par) yield fact_im(n) 
   335     val l2 = for (n <- (List.fill(100000)(20 to 21)).flatten.par) yield fact_im(n) 
   325     l1.sum - l2.sum
   336     l1.sum - l2.sum
   326   }
   337   }
   327 }
   338 }
   328 
   339 
   329 test().sum
   340 (for (i <- (1 to 10)) yield test().sum).sum
   330 println(l1.sum - l2.sum)
   341 
   331 
   342 
   332 // Webpages
   343 // Webpages
   333 //==========
   344 //==========
   334 
   345 
   335 import io.Source
   346 import io.Source
   531   plan.par.map(i(_)).reduce(_ + _)
   542   plan.par.map(i(_)).reduce(_ + _)
   532 
   543 
   533 santa_state("(((((()))))".toList)
   544 santa_state("(((((()))))".toList)
   534 santa_imutable("(((((()))))".toList)
   545 santa_imutable("(((((()))))".toList)
   535 
   546 
   536 def randomString(length: Int) = 
   547 def randomString(length: Int) : String = 
   537   List.fill(length)((40 + scala.util.Random.nextInt(2)).toChar)
   548   List.fill(length)((40 + scala.util.Random.nextInt(2)).toChar)
   538 
   549 
   539 
   550 
   540 santa_state(randomString(100))
   551 santa_state(randomString(100))
   541 
   552