progs/lecture1.scala
changeset 313 dea46bdfd648
parent 310 335079d938aa
child 314 21b52310bd8b
equal deleted inserted replaced
312:d8a15207114b 313:dea46bdfd648
    68 Set(1,2,3) == Set(3,1,2)
    68 Set(1,2,3) == Set(3,1,2)
    69 List(1,2,3) == List(3,1,2)
    69 List(1,2,3) == List(3,1,2)
    70 
    70 
    71 
    71 
    72 // this applies to "concrete" values...pretty much everything;
    72 // this applies to "concrete" values...pretty much everything;
    73 // but for example you cannot compare functions (later)
    73 // but for example you cannot compare functions (later),
       
    74 // and also not Arrays
       
    75 
       
    76 Array(1) == Array(1)
    74 
    77 
    75 
    78 
    76 // Printing/Strings
    79 // Printing/Strings
    77 //==================
    80 //==================
    78 
    81 
    79 println("test")
    82 println("test")
    80 
    83 
    81 val tst = "This is a " ++ "test" 
    84 val tst = "This is a " ++ "test" 
    82 
    85 
    83 print(tst)
    86 print(tst) 
    84 println(tst)
    87 println(tst) 
    85 
    88 
    86 val lst = List(1,2,3,1)
    89 val lst = List(1,2,3,1)
    87 
    90 
    88 
    91 
    89 println(lst.toString)
    92 println(lst.toString)
   210 //  }
   213 //  }
   211 
   214 
   212 
   215 
   213 //
   216 //
   214 // BTW: no returns!!
   217 // BTW: no returns!!
   215 // "last" line (expression) in a function determines the result
   218 // "last" line (expression) in a function determines the 
   216 //
   219 // result
       
   220 
   217 
   221 
   218 def silly(n: Int) : Int = {
   222 def silly(n: Int) : Int = {
   219   if (n < 10) n * n
   223   if (n < 10) n * n
   220   else n + n
   224   else n + n
   221 }
   225 }
   400 // Remember:
   404 // Remember:
   401 // - no vars, no ++i, no +=
   405 // - no vars, no ++i, no +=
   402 // - no mutable data-structures (no Arrays, no ListBuffers)
   406 // - no mutable data-structures (no Arrays, no ListBuffers)
   403 
   407 
   404 // But what the heck....
   408 // But what the heck....
   405 // Q: Count how many elements are in the intersections of two sets?
   409 // Q: Count how many elements are in the intersections of 
       
   410 //    two sets?
   406 // A; IMPROPER WAY (mutable counter)
   411 // A; IMPROPER WAY (mutable counter)
   407 
   412 
   408 def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
   413 def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
   409   var count = 0
   414   var count = 0
   410   for (x <- A.par; if (B contains x)) count += 1 
   415   for (x <- A.par; if (B contains x)) count += 1