From d8e4551abaaff97f334278331bee002a0aa573be Mon Sep 17 00:00:00 2001
From: Etienne Kneuss <colder@php.net>
Date: Mon, 18 Aug 2014 17:00:10 +0200
Subject: [PATCH] Add isSorted/sorted to ListOps for Lists of ints

---
 library/collection/List.scala | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/library/collection/List.scala b/library/collection/List.scala
index 69383585c..5720d635f 100644
--- a/library/collection/List.scala
+++ b/library/collection/List.scala
@@ -279,9 +279,31 @@ sealed abstract class List[T] {
 @library
 object ListOps {
   def flatten[T](ls: List[List[T]]): List[T] = ls match {
-    case Cons(l, t) => l ++ flatten(t)
+    case Cons(h, t) => h ++ flatten(t)
     case Nil() => Nil()
   }
+
+  def isSorted(ls: List[Int]): Boolean = ls match {
+    case Nil() => true
+    case Cons(_, Nil()) => true
+    case Cons(h1, Cons(h2, _)) if(h1 > h2) => false
+    case Cons(_, t) => isSorted(t)
+  }
+
+  def sorted(ls: List[Int]): List[Int] = ls match {
+    case Cons(h, t) => insSort(sorted(t), h)
+    case Nil() => Nil()
+  }
+
+  def insSort(ls: List[Int], v: Int): List[Int] = ls match {
+    case Nil() => Cons(v, Nil())
+    case Cons(h, t) =>
+      if (v <= h) {
+        Cons(v, t)
+      } else {
+        Cons(h, insSort(t, v))
+      }
+  }
 }
 
 
-- 
GitLab