Slide 8
Slide 8 text
۩ମྫίϯύϨʔλ
&YBNQMF$PNQBSBUPS
4DBMBͰͷ࣮ྫ
trait Comparator[T] {
def compare(a: T, b: T): Int
}
object IntComparator extends Comparator[Int] {
def compare(a: Int, b: Int): Int = a - b
}
def sort[T](list: List[T], comparator: Comparator[T]): List[T] = {
list match {
case x :: xs =>
val (ys, zs) = xs.partition(a => comparator.compare(x, a) > 0)
sort(ys, comparator) ++ List(x) ++ sort(zs, comparator)
case Nil => Nil
}
}
sort(List(6, 1, 4, 7, 5, 2, 9, 3, 8), IntComparator)