summaryrefslogtreecommitdiff
path: root/sources/examples/sort2.scala
diff options
context:
space:
mode:
authormichelou <michelou@epfl.ch>2003-11-28 17:04:18 +0000
committermichelou <michelou@epfl.ch>2003-11-28 17:04:18 +0000
commitdaea8b76a5f9dd35e54f4d524691187850202182 (patch)
tree69409f806b178c804446def99b96f7a017d3cdf9 /sources/examples/sort2.scala
parent6b8d116ec9f10e97fd2fb3baf7ebd99af0ebedf2 (diff)
downloadscala-daea8b76a5f9dd35e54f4d524691187850202182.tar.gz
scala-daea8b76a5f9dd35e54f4d524691187850202182.tar.bz2
scala-daea8b76a5f9dd35e54f4d524691187850202182.zip
- updated to current Scala compiler
- added main() for testing
Diffstat (limited to 'sources/examples/sort2.scala')
-rw-r--r--sources/examples/sort2.scala31
1 files changed, 21 insertions, 10 deletions
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