Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 13th, 2012  |  syntax: None  |  size: 0.92 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Scala by Example - problems with currying
  2. def msort[A](less: (A, A) => Boolean)(xs: List[A]): List[A] = {
  3.   def merge(xs1: List[A], xs2: List[A]): List[A] =
  4.     if (xs1.isEmpty) xs2
  5.     else if (xs2.isEmpty) xs1
  6.     else if (less(xs1.head, xs2.head)) xs1.head :: merge(xs1.tail, xs2)
  7.     else xs2.head :: merge(xs1, xs2.tail)
  8.   val n = xs.length/2
  9.   if (n == 0) xs
  10.   else merge(msort(less)(xs take n), msort(less)(xs drop n))
  11. }
  12.        
  13. val intSort = msort((x : Int, y : Int) => x < y)
  14. val reverseSort = msort((x:Int, y:Int) => x > y)
  15.        
  16. val intSort = msort((x : Int, y : Int) => x < y)(List(1, 2, 4))
  17. val reverseSort = msort((x:Int, y:Int) => x > y)(List(4, 3, 2))
  18.        
  19. error: missing arguments for method msort in object $iw;
  20. follow this method with `_' if you want to treat it as a partially applied function
  21.        
  22. val intSort = msort((x : Int, y : Int) => x < y) _
  23.        
  24. val intSort: List[Int] => List[Int] = msort((x: Int, y: Int) => x < y)