progs/lecture2.scala
changeset 362 1bde878ba6c9
parent 361 f88b5cec2e5d
child 363 e5c1d69cffa4
equal deleted inserted replaced
361:f88b5cec2e5d 362:1bde878ba6c9
   208 
   208 
   209 def even(x: Int) : Boolean = x % 2 == 0
   209 def even(x: Int) : Boolean = x % 2 == 0
   210 def odd(x: Int) : Boolean = x % 2 == 1
   210 def odd(x: Int) : Boolean = x % 2 == 1
   211 
   211 
   212 val lst = (1 to 10).toList
   212 val lst = (1 to 10).toList
   213 lst.reverse.sorted
       
   214 
       
   215 
   213 
   216 lst.filter(even)
   214 lst.filter(even)
   217 lst.count(odd)
   215 lst.count(odd)
   218 lst.find(even)
   216 lst.find(even)
   219 lst.exists(even)
   217 lst.exists(even)
   220 
   218 
       
   219 lst.find(_ < 4)
   221 lst.filter(_ < 4) 
   220 lst.filter(_ < 4) 
   222 lst.filter(x => x % 2 == 1)
   221 
       
   222 def less4(x: Int) : Boolean = x < 4
       
   223 lst.find(less4)
       
   224 lst.find(_ < 4)
       
   225 
       
   226 lst.filter(x => x % 2 == 0)
   223 lst.filter(_ % 2 == 0)
   227 lst.filter(_ % 2 == 0)
   224 
   228 
   225 
   229 
   226 lst.sortWith((x, y) => x > y)
   230 lst.sortWith((x, y) => x < y)
   227 lst.sortWith(_ < _)
   231 lst.sortWith(_ > _)
   228 
   232 
   229 // but this only works when the arguments are clear, but 
   233 // but this only works when the arguments are clear, but 
   230 // not with multiple occurences
   234 // not with multiple occurences
   231 lst.find(n => odd(n) && n > 2)
   235 lst.find(n => odd(n) && n > 2)
   232 
   236 
   233 
   237 
   234 val ps = List((3, 0), (3, 2), (4, 2), (2, 2), (2, 0), (1, 1), (1, 0))
   238 
       
   239 val ps = List((3, 0), (3, 2), (4, 2), (2, 2), 
       
   240               (2, 0), (1, 1), (1, 0))
   235 
   241 
   236 def lex(x: (Int, Int), y: (Int, Int)) : Boolean = 
   242 def lex(x: (Int, Int), y: (Int, Int)) : Boolean = 
   237   if (x._1 == y._1) x._2 < y._2 else x._1 < y._1
   243   if (x._1 == y._1) x._2 < y._2 else x._1 < y._1
   238 
   244 
   239 ps.sortWith(lex)
   245 ps.sortWith(lex)
   249 //===================
   255 //===================
   250 
   256 
   251 def double(x: Int): Int = x + x
   257 def double(x: Int): Int = x + x
   252 def square(x: Int): Int = x * x
   258 def square(x: Int): Int = x * x
   253 
   259 
   254 
       
   255 val lst = (1 to 10).toList
   260 val lst = (1 to 10).toList
   256 
   261 
       
   262 lst.map(square)
   257 lst.map(x => (double(x), square(x)))
   263 lst.map(x => (double(x), square(x)))
   258 
   264 
   259 lst.map(square)
   265 // works also for strings
       
   266 def tweet(c: Char) = c.toUpper
       
   267 
       
   268 "Hello World".map(tweet)
       
   269 
       
   270 
       
   271 // this can be iterated
       
   272 
       
   273 lst.map(square).filter(_ > 4)
       
   274 
       
   275 (lst.map(square)
       
   276    .find(_ > 4)
       
   277    .map(square))
       
   278 
       
   279 lst.map(square).find(_ > 4)
   260 
   280 
   261 // this is actually how for-comprehensions are
   281 // this is actually how for-comprehensions are
   262 // defined in Scala
   282 // defined in Scala
   263 
   283 
   264 lst.map(n => square(n))
   284 lst.map(n => square(n))
   265 for (n <- lst) yield square(n)
   285 for (n <- lst) yield square(n)
   266 
   286 
   267 // this can be iterated
       
   268 
       
   269 lst.map(square).filter(_ > 4)
       
   270 
       
   271 (lst.map(square)
       
   272    .filter(_ > 4)
       
   273    .map(square))
       
   274 
       
   275 
       
   276 // lets define our own higher-order functions
   287 // lets define our own higher-order functions
   277 // type of functions is for example Int => Int
   288 // type of functions is for example Int => Int
   278 
       
   279 
   289 
   280 
   290 
   281 def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = {
   291 def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = {
   282   if (lst == Nil) Nil
   292   if (lst == Nil) Nil
   283   else f(lst.head) :: my_map_int(lst.tail, f)
   293   else f(lst.head) :: my_map_int(lst.tail, f)
   288 
   298 
   289 // same function using pattern matching: a kind
   299 // same function using pattern matching: a kind
   290 // of switch statement on steroids (see more later on)
   300 // of switch statement on steroids (see more later on)
   291 
   301 
   292 def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
   302 def my_map_int(lst: List[Int], f: Int => Int) : List[Int] = 
   293 lst match {
   303   lst match {
   294   case Nil => Nil
   304     case Nil => Nil
   295   case x::xs => f(x)::my_map_int(xs, f)
   305     case x::xs => f(x)::my_map_int(xs, f)
   296 }
   306   }
   297 
   307 
   298 
   308 
   299 // other function types
   309 // other function types
   300 //
   310 //
   301 // f1: (Int, Int) => Int
   311 // f1: (Int, Int) => Int
   691 
   701 
   692 
   702 
   693 
   703 
   694 /*
   704 /*
   695  *               1
   705  *               1
   696   *             / |  \
   706  *             / |  \
   697   *           /   |   \
   707  *           /   |   \
   698   *         /     |    \
   708  *         /     |    \
   699   *        2      3     8
   709  *        2      3     8
   700   *      /  \    / \   / \
   710  *      /  \    / \   / \
   701   *     4    5  6   7 9  10
   711  *     4    5  6   7 9  10
   702   * Preorder: 1,2,4,5,3,6,7,8,9,10
   712  * Preorder: 1,2,4,5,3,6,7,8,9,10
   703   * InOrder: 4,2,5,1,6,3,7,9,8,10
   713  * InOrder: 4,2,5,1,6,3,7,9,8,10
   704   * PostOrder: 4,5,2,6,7,3,9,10,8,1
   714  * PostOrder: 4,5,2,6,7,3,9,10,8,1
   705   *
   715  *
   706  
   716  
   707 show inorder, preorder, postorder
   717 show inorder, preorder, postorder
   708 
   718 
   709 
   719 
   710 
   720