From daea8b76a5f9dd35e54f4d524691187850202182 Mon Sep 17 00:00:00 2001 From: michelou Date: Fri, 28 Nov 2003 17:04:18 +0000 Subject: - updated to current Scala compiler - added main() for testing --- sources/examples/sort2.scala | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'sources/examples/sort2.scala') diff --git a/sources/examples/sort2.scala b/sources/examples/sort2.scala index a4e32a9cd4..f143958471 100644 --- a/sources/examples/sort2.scala +++ b/sources/examples/sort2.scala @@ -1,12 +1,23 @@ -module sorter { +object sorter { + + def sort(a: List[Int]): List[Int] = { + if (a.length < 2) + a + else { + val pivot = a(a.length / 2); + def lePivot(x: Int) = x < pivot; + def gtPivot(x: Int) = x > pivot; + def eqPivot(x: Int) = x == pivot; + sort(a filter lePivot) + ::: sort(a filter eqPivot) + ::: sort(a filter gtPivot) + } + } + + def main(args: Array[String]) = { + val xs = List(6, 2, 8, 5, 1); + Console.println(xs); + Console.println(sort(xs)); + } -def sort (a: List[Int]): List[Int] = { - val pivot = a at (a.length / 2); - def leqPivot(x: Int) = x <= pivot; - def gtPivot(x: Int) = x > pivot; - def eqPivot(x: Int) = x == pivot; - sort(a filter leqPivot) - ::: sort(a filter eqPivot) - ::: sort(a filter gtPivot) -} } \ No newline at end of file -- cgit v1.2.3