progs/lecture1.scala
changeset 314 21b52310bd8b
parent 313 dea46bdfd648
child 316 8b57dd326a91
equal deleted inserted replaced
313:dea46bdfd648 314:21b52310bd8b
     3 
     3 
     4 // Value assignments
     4 // Value assignments
     5 // (their names should be lower case)
     5 // (their names should be lower case)
     6 //====================================
     6 //====================================
     7 
     7 
       
     8 //var z = 9
       
     9 //z = 10
     8 
    10 
     9 val x = 42
    11 val x = 42
    10 val y = 3 + 4
    12 val y = 3 + 4
    11 val z = x / y
    13 val z = x / y
       
    14 val x = 70
    12 
    15 
    13 // (you cannot reassign values: z = 9 will give an error)
    16 // (you cannot reassign values: z = 9 will give an error)
    14 
    17 
    15 
    18 
    16 // Hello World
    19 // Hello World
    39 Set(1,2,3,1)
    42 Set(1,2,3,1)
    40 
    43 
    41 // ranges
    44 // ranges
    42 1 to 10
    45 1 to 10
    43 (1 to 10).toList
    46 (1 to 10).toList
       
    47 (1 to 10).toList.toString
    44 
    48 
    45 (1 until 10).toList
    49 (1 until 10).toList
    46 
    50 
    47 // picking an element in a list
    51 // picking an element in a list
    48 val lst = List(1, 2, 3, 1)
    52 val lst = List(1, 2, 3, 1)
    67 
    71 
    68 Set(1,2,3) == Set(3,1,2)
    72 Set(1,2,3) == Set(3,1,2)
    69 List(1,2,3) == List(3,1,2)
    73 List(1,2,3) == List(3,1,2)
    70 
    74 
    71 
    75 
    72 // this applies to "concrete" values...pretty much everything;
    76 // this applies to "concrete" values...pretty much 
    73 // but for example you cannot compare functions (later),
    77 // everything; but for example you cannot compare 
    74 // and also not Arrays
    78 // functions (later), and also not arrays
    75 
    79 
    76 Array(1) == Array(1)
    80 Array(1) == Array(1)
    77 
    81 
    78 
    82 
    79 // Printing/Strings
    83 // Printing/Strings
    95 println(lst.mkString(","))
    99 println(lst.mkString(","))
    96 
   100 
    97 println(lst.mkString(", "))
   101 println(lst.mkString(", "))
    98 
   102 
    99 // some methods take more than one argument
   103 // some methods take more than one argument
       
   104 
   100 println(lst.mkString("{", ",", "}"))
   105 println(lst.mkString("{", ",", "}"))
   101 
   106 
   102 // (in this case .mkString can take no, one, 
   107 // (in this case .mkString can take no, one, 
   103 // or three arguments...this has to do with
   108 // or three arguments...this has to do with
   104 // default arguments)
   109 // default arguments)
   164 // you can make the type of a value explicit
   169 // you can make the type of a value explicit
   165 val name: String = "leo"
   170 val name: String = "leo"
   166 
   171 
   167 
   172 
   168 // type errors
   173 // type errors
   169 math.sqrt("64")
   174 math.sqrt("64".toDouble)
   170 
   175 
   171 // produces
   176 // produces
   172 //
   177 //
   173 // error: type mismatch;
   178 // error: type mismatch;
   174 // found   : String("64")
   179 // found   : String("64")
   191 
   196 
   192 
   197 
   193 // Function Definitions
   198 // Function Definitions
   194 //======================
   199 //======================
   195 
   200 
       
   201 def foo(s: String) : String = {
       
   202   val tmp = s ++ s ++ s
       
   203   val res = s ++ s
       
   204   res
       
   205 }
       
   206 
       
   207 
       
   208 foo("test")
       
   209 
   196 def incr(x: Int) : Int = x + 1
   210 def incr(x: Int) : Int = x + 1
   197 def double(x: Int) : Int = x + x
   211 def double(x: Int) : Int = x + x
   198 def square(x: Int) : Int = x * x
   212 def square(x: Int) : Int = x * x
   199 
   213 
   200 def str(x: Int) : String = x.toString
   214 def str(x: Int) : String = x.toString
   203 double(4)
   217 double(4)
   204 square(6)
   218 square(6)
   205 str(3)
   219 str(3)
   206 
   220 
   207 
   221 
   208 // The general scheme for a function: you have to give a type 
   222 // The general scheme for a function: you have to give a 
   209 // to each argument and a return type of the function
   223 // type to each argument and a return type of the function
   210 //
   224 //
   211 //  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
   225 //  def fname(arg1: ty1, arg2: ty2,..., argn: tyn): rty = {
   212 //    body 
   226 //    
   213 //  }
   227 //  }
   214 
   228 
   215 
   229 
   216 //
       
   217 // BTW: no returns!!
   230 // BTW: no returns!!
   218 // "last" line (expression) in a function determines the 
   231 // "last" line (expression) in a function determines the 
   219 // result
   232 // result
   220 
   233 
   221 
   234 
   248 def fact2(n: BigInt) : BigInt = 
   261 def fact2(n: BigInt) : BigInt = 
   249   if (n == 0) 1 else n * fact2(n - 1)
   262   if (n == 0) 1 else n * fact2(n - 1)
   250 
   263 
   251 fact2(150)
   264 fact2(150)
   252 
   265 
   253 
       
   254 def fib(n: Int) : Int =
   266 def fib(n: Int) : Int =
   255   if (n == 0) 1 else
   267   if (n == 0) 1 else
   256     if (n == 1) 1 else fib(n - 1) + fib(n - 2)
   268     if (n == 1) 1 else fib(n - 1) + fib(n - 2)
   257 
   269 
   258 
   270 
   274 
   286 
   275 
   287 
   276 // For-Comprehensions (not For-Loops)
   288 // For-Comprehensions (not For-Loops)
   277 //====================================
   289 //====================================
   278 
   290 
   279 
   291 (1 to 10).toList
   280 for (n <- (1 to 10).toList) yield { 
   292 for (n <- (1 to 10).toList) yield { 
   281   square(n) + 1
   293   square(n) + 1
   282 }
   294 }
   283 
   295 
   284 for (n <- (1 to 10).toList; 
   296 for (n <- (1 to 10).toList; 
   285      m <- (1 to 10).toList) yield m * n
   297      m <- (1 to 10).toList) yield (m, n)
   286 
   298 
   287 
   299 
   288 // you can assign the result of a for-comprehension
   300 // you can assign the result of a for-comprehension
   289 // to a value
   301 // to a value
   290 val mult_table = 
   302 val mult_table = 
   294 println(mult_table.mkString)
   306 println(mult_table.mkString)
   295 mult_table.sliding(10,10).mkString("\n")
   307 mult_table.sliding(10,10).mkString("\n")
   296 
   308 
   297 // the list/set/... can also be constructed in any 
   309 // the list/set/... can also be constructed in any 
   298 // other way
   310 // other way
       
   311 
   299 for (n <- Set(10,12,4,5,7,8,10)) yield n * n
   312 for (n <- Set(10,12,4,5,7,8,10)) yield n * n
   300 
   313 
       
   314 for (n <- (1 to 10)) yield {
       
   315   n * n  
       
   316 }
       
   317 
       
   318 if (1 == 2) "a" else "b"
   301 
   319 
   302 // with if-predicates / filters
   320 // with if-predicates / filters
   303 
   321 
   304 for (n <- (1 to 3).toList; 
   322 for (n <- (1 to 3).toList; 
   305      m <- (1 to 3).toList;
   323      m <- (1 to 3).toList;
   306      if (n + m) % 2 == 0;
   324      if (n + m) % 2 == 0) yield (n, m)
   307      if (n * m) < 2) yield (n, m)
       
   308 
   325 
   309 for (n <- (1 to 3).toList; 
   326 for (n <- (1 to 3).toList; 
   310      m <- (1 to 3).toList;
   327      m <- (1 to 3).toList;
   311      if ((((n + m) % 2 == 0)))) yield (n, m)
   328      if ((((n + m) % 2 == 0)))) yield (n, m)
   312 
   329 
   328 }
   345 }
   329 
   346 
   330 // Functions producing multiple outputs
   347 // Functions producing multiple outputs
   331 //======================================
   348 //======================================
   332 
   349 
   333 def get_ascii(c: Char) : (Char, Int) = (c, c.toInt)
   350 def get_ascii(c: Char) : (Char, Int) = 
       
   351   (c, c.toInt)
   334 
   352 
   335 get_ascii('a')
   353 get_ascii('a')
   336 
   354 
   337 
   355 
   338 // .maxBy, sortBy with pairs
   356 // .maxBy, sortBy with pairs
   339 def get_length(s: String) : (String, Int) = (s, s.length) 
   357 def get_length(s: String) : (String, Int) = 
       
   358   (s, s.length) 
   340 
   359 
   341 val lst = List("zero", "one", "two", "three", "four", "ten")
   360 val lst = List("zero", "one", "two", "three", "four", "ten")
   342 val strs = for (s <- lst) yield get_length(s)
   361 val strs = for (s <- lst) yield get_length(s)
   343 
   362 
   344 strs.sortBy(_._2)
   363 strs.sortBy(_._2)
   354 // with only a side-effect (no list is produced),
   373 // with only a side-effect (no list is produced),
   355 // has no "yield"
   374 // has no "yield"
   356 
   375 
   357 for (n <- (1 to 10)) println(n)
   376 for (n <- (1 to 10)) println(n)
   358 
   377 
   359 
   378 (1 to 10).toList
       
   379 (1 until 10).toList
   360 // BTW: a roundabout way of printing out a list, say
   380 // BTW: a roundabout way of printing out a list, say
   361 val lst = ('a' to 'm').toList
   381 val lst = ('a' to 'm').toList
   362 
   382 
   363 for (i <- (0 until lst.length)) println(lst(i))
   383 for (i <- (0 until lst.length)) println(lst(i))
   364 
   384