diff lib/ur/list.ur @ 1321:4172863d049d

queryL1 and List.sort
author Adam Chlipala <adam@chlipala.net>
date Sat, 20 Nov 2010 10:45:22 -0500
parents 4c367c8f5b2d
children 80bff6449f41
line wrap: on
line diff
--- a/lib/ur/list.ur	Sat Nov 20 09:48:03 2010 -0500
+++ b/lib/ur/list.ur	Sat Nov 20 10:45:22 2010 -0500
@@ -280,6 +280,34 @@
                 [];
     return (rev ls)
 
+fun sort [a] (gt : a -> a -> bool) (ls : t a) : t a =
+    let
+        fun split ls acc1 acc2 =
+            case ls of
+                [] => (rev acc1, rev acc2)
+              | x :: [] => (rev (x :: acc1), rev acc2)
+              | x1 :: x2 :: ls' => split ls' (x1 :: acc1) (x2 :: acc2)
+
+        fun merge ls1 ls2 acc =
+            case (ls1, ls2) of
+                ([], _) => revAppend acc ls2
+              | (_, []) => revAppend acc ls1
+              | (x1 :: ls1', x2 :: ls2') => if gt x1 x2 then merge ls1 ls2' (x2 :: acc) else merge ls1' ls2 (x1 :: acc)
+
+        fun sort' ls =
+            case ls of
+                [] => ls
+              | _ :: [] => ls
+              | _ =>
+                let
+                    val (ls1, ls2) = split ls [] []
+                in
+                    merge (sort' ls1) (sort' ls2) []
+                end
+    in
+        sort' ls
+    end
+
 fun assoc [a] [b] (_ : eq a) (x : a) =
     let
         fun assoc' (ls : list (a * b)) =