msort vs sortWith

val ls = List("a", "b", "c", "d", "e")
msort((x: Char, y: Char) => x > y)(ls)
ls sortWith(_ > _)
msort(_ > _)(ls)    // error: missing parameter type for expanded   
scala类型推导

scala类型推导

// my msort
def msort[T](fn: (T, T) => Boolean)(ls: List[T]): List[T] = {

  def merge(l1: List[T], l2: List[T]): List[T] = {    // 这里不能指定merge的范型即merge[T](...), 
    //  不然会报错:found   : T(in method merge)
    //                     required: T(in method msort)
    //                                         else if(fn(l1.head, l2.head))
    if (l1.isEmpty)
      l2
    else if(l2.isEmpty)
      l1
    else if(fn(l1.head, l2.head))
      l1.head :: merge(l1.tail, l2)
    else
      l2.head :: merge(l1, l2.tail)
  }

  if(ls.length >= 2){
    val (s1, s2) = ls.splitAt(ls.length/2)
    merge(msort(fn)(s1), msort(fn)(s2))
  }
  else {
    ls
  }
}