progs/lecture3.scala
changeset 343 c8fcc0e0a57f
parent 335 7e00d2b13b04
child 364 f1a6fa599d26
equal deleted inserted replaced
342:aa47abb9e723 343:c8fcc0e0a57f
    31 }
    31 }
    32 
    32 
    33 val l = List((1,2), (5,3), (2,5), (1,3))
    33 val l = List((1,2), (5,3), (2,5), (1,3))
    34 
    34 
    35 l.map(test).flatten
    35 l.map(test).flatten
       
    36 
       
    37 // naive quicksort with "On" function
       
    38 
       
    39 def sortOn(f: Int => Int, xs: List[Int]) : List[Int] = {
       
    40   if (xs.size < 2) xs
       
    41   else {
       
    42    val pivot = xs.head
       
    43    val (left, right) = xs.partition(f(_) < f(pivot))
       
    44    sortOn(f, left) ::: pivot :: sortOn(f, right.tail)
       
    45   }
       
    46 } 
       
    47 
       
    48 sortOn(identity, List(99,99,99,98,10,-3,2)) 
       
    49 sortOn(n => - n, List(99,99,99,98,10,-3,2))
       
    50 
       
    51 
       
    52 
    36 
    53 
    37 // Recursion Again ;o)
    54 // Recursion Again ;o)
    38 //====================
    55 //====================
    39 
    56 
    40 
    57