[MLton-commit] r5393

geoffw at mlton.org geoffw at mlton.org
Mon Mar 5 10:52:29 PST 2007


Added uniqueByEq, divideByEq, and nubByEq to List/LIST.

Added signatures for state monads (and modified the exports 
to make them visible): MONAD_WS, MONAD_STATE, MONADP_STATE.


----------------------------------------------------------------------

U   mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml
U   mltonlib/trunk/com/ssh/extended-basis/unstable/public/concept/monad.sig
U   mltonlib/trunk/com/ssh/extended-basis/unstable/public/export/common.sml
U   mltonlib/trunk/com/ssh/extended-basis/unstable/public/sequence/list.sig

----------------------------------------------------------------------

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml	2007-03-05 17:37:07 UTC (rev 5392)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml	2007-03-05 18:23:49 UTC (rev 5393)
@@ -96,4 +96,38 @@
       fun takeWhile ? = rev o #1 o mk op :: [] ?
       fun dropWhile ? = #2 o mk ignore () ?
    end
+   
+   fun uniqueByEq eq xs = 
+       case xs
+         of [] => true
+          | x::xs' => 
+            exists (Fn.curry eq x) xs' andalso
+            uniqueByEq eq xs' 
+
+   local
+     fun divideByEqTail eq xs accum = 
+         case xs
+           of [] => rev accum 
+            | x::xs' => let
+                val (xclass, remainder) = partition (Fn.curry eq x) xs'
+              in
+                divideByEqTail eq remainder ((x::xclass)::accum)
+              end
+   in
+     fun divideByEq eq xs = divideByEqTail eq xs []
+   end
+
+   local
+     fun nubByEqTail eq xs accum = 
+         case xs
+           of [] => accum
+            | x::xs' =>
+                if exists (Fn.curry eq x) xs' then
+                  nubByEqTail eq xs' accum
+                else 
+                  nubByEqTail eq xs' (x::accum)   
+   in
+     fun nubByEq eq xs = nubByEqTail eq (rev xs) []
+   end
+
 end

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/public/concept/monad.sig
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/public/concept/monad.sig	2007-03-05 17:37:07 UTC (rev 5392)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/concept/monad.sig	2007-03-05 18:23:49 UTC (rev 5393)
@@ -64,3 +64,23 @@
    include MONAD_EX where type 'a monad_ex = 'a monad
    include MONADP_EX where type 'a monadp_ex = 'a monad
 end
+
+(** == State Monads == *)
+
+signature MONAD_WS = sig (* WS = WITH_STATE *)
+  type 'a monad_ws
+  type monad_ws_state
+  val getState : monad_ws_state monad_ws
+  val setState : monad_ws_state -> Unit.t monad_ws
+  val run : monad_ws_state -> 'a monad_ws -> monad_ws_state * 'a
+end 
+ 
+signature MONAD_STATE = sig
+  include MONAD
+  include MONAD_WS where type 'a monad_ws = 'a monad
+end 
+
+signature MONADP_STATE = sig
+  include MONADP
+  include MONAD_WS where type 'a monad_ws = 'a monad
+end 

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/public/export/common.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/public/export/common.sml	2007-03-05 17:37:07 UTC (rev 5392)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/export/common.sml	2007-03-05 18:23:49 UTC (rev 5393)
@@ -25,6 +25,9 @@
 signature MONADP = MONADP
 signature MONADP_CORE = MONADP_CORE
 signature MONAD_CORE = MONAD_CORE
+signature MONAD_WS = MONAD_WS
+signature MONAD_STATE = MONAD_STATE
+signature MONADP_STATE = MONADP_STATE
 signature ORDERED = ORDERED
 signature ORDERED_CORE = ORDERED_CORE
 signature SCANNABLE = SCANNABLE

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/public/sequence/list.sig
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/public/sequence/list.sig	2007-03-05 17:37:07 UTC (rev 5392)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/sequence/list.sig	2007-03-05 18:23:49 UTC (rev 5393)
@@ -117,4 +117,32 @@
     * Given an equality predicate on an element type returns an equality
     * predicate on lists of the element type.
     *)
+
+   (** == Operations using equivalence relations and partial orders == 
+    * The {ByEq} functions use a binary predicate and operates in O(n^2) 
+    * time.  The binary predicate is assumed to be an equivalence relation.  
+    *
+    * The {ByCmp} use comparison function and operates in O(n log n) time.  
+    * The comparison function is assumed to be be partial order.
+    *)
+
+   val uniqueByEq : 'a BinPr.t -> 'a t UnPr.t
+   (** 
+    * {uniqueByEq eq xs} returns {true} all if elements of are pair-wise 
+    * distinct.  
+    *)
+   
+   val divideByEq : 'a BinPr.t -> 'a t -> 'a t t
+   (** 
+    * {divideByEq eq xs} divides {xs} up into a list of lists. Each list
+    * contains elements in the equivalence class induced by {eq}.  
+    *)
+
+   val nubByEq : 'a BinPr.t -> 'a t -> 'a t
+   (** 
+    * {nubByEq eq xs} removes duplicates in {xs} based upon the 
+    * equivalence class specified by {eq}.  It preserves the ordering of 
+    * the elements in {xs}.
+    *)
+
 end




More information about the MLton-commit mailing list