[MLton-commit] r4835

Vesa Karvonen vesak at mlton.org
Sun Nov 19 09:20:56 PST 2006


Added some more functions on lists.
----------------------------------------------------------------------

U   mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml
U   mltonlib/trunk/com/ssh/extended-basis/unstable/public/list.sig

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

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml	2006-11-19 03:02:09 UTC (rev 4834)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/list.sml	2006-11-19 17:20:55 UTC (rev 4835)
@@ -11,4 +11,68 @@
    open List
    type 'a t = 'a list
    val sub = nth
+   fun init l = rev (tl (rev l))
+   fun intersperse d =
+       fn [] => [] | x::xs => x::rev (foldl (fn (x, ys) => x::d::ys) [] xs)
+   local
+      fun cross (f, g) (x, y) = (f x, g y)
+      fun headsAndTails xss =
+          cross (rev, rev)
+                (foldl (fn (h::t, (hs, ts)) => (h::hs, t::ts) | ([], ?) => ?)
+                       ([], []) xss)
+   in
+      fun transpose xss =
+          case xss of
+             [] => []
+           | []::xss => transpose xss
+           | (x::xs)::xss => let
+                val (hs, ts) = headsAndTails xss
+             in
+                (x::hs)::transpose (xs::ts)
+             end
+   end
+   fun foldl1 f = fn [] => raise Empty | x::xs => foldl f x xs
+   fun foldr1 f = foldl1 f o rev
+   fun push (r, x) = r := x :: !r
+   fun pop r = case !r of x::xs => (r := xs ; SOME x) | [] => NONE
+   fun split (l, i) = let
+      fun lp (hs,    ts, 0) = (rev hs, ts)
+        | lp (_,     [], _) = raise Subscript
+        | lp (hs, t::ts, n) = lp (t::hs, ts, n-1)
+   in
+      if i < 0 then raise Subscript else lp ([], l, i)
+   end
+   fun findi p l = let
+      fun lp (_, []) = NONE
+        | lp (i, x::xs) = if p (i, x) then SOME (i, x) else lp (i+1, xs)
+   in
+      lp (0, l)
+   end
+   fun equal eq = let
+      fun lp ([],       []) = true
+        | lp (x::xs, y::ys) = eq (x, y) andalso lp (xs, ys)
+        | lp (_,         _) = false
+   in
+      lp
+   end
+   fun concatMap f = rev o foldl (fn (x, ys) => revAppend (f x, ys)) []
+   fun appr e = app e o rev
+   fun foldli f y = #2 o foldl (fn (x, (i, y)) => (i+1, f (i+1, x, y))) (~1, y)
+   fun foldri f y xs = let
+      val (n, xs) = foldl (fn (x, (n, xs)) => (n+1, x::xs)) (0, []) xs
+   in
+      #2 (foldl (fn (x, (i, y)) => (i-1, f (i-1, x, y))) (n, y) xs)
+   end
+   fun concatMapi f =
+       rev o foldli (fn (i, x, ys) => revAppend (f (i, x), ys)) []
+   fun mapiPartial f =
+       rev o foldli (fn (i, x, ys) => case f (i, x) of NONE => ys
+                                                     | SOME y => y::ys) []
+   fun mapi f = mapiPartial (SOME o f)
+   fun appi e = foldli (fn (i, x, ()) => e (i, x)) ()
+   fun appri e = foldri (fn (i, x, ()) => e (i, x)) ()
+   fun existsi p = Option.isSome o findi p
+   fun alli p = Option.isNone o findi (not o p)
+   fun index ? = mapi (fn ? => ?) ?
+   fun contains l x = exists (fn y => x = y) l
 end

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/public/list.sig
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/public/list.sig	2006-11-19 03:02:09 UTC (rev 4834)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/list.sig	2006-11-19 17:20:55 UTC (rev 4835)
@@ -11,13 +11,81 @@
    include LIST
 
    type 'a t = 'a list
-   (**
-    * Convenience alias.
-    *)
+   (** Convenience alias. *)
 
-   val sub : 'a list * int -> 'a
+   (** == Basic == *)
+
+   val sub : 'a t * int -> 'a
    (**
     * {sub (l, i)} returns the {i}th element of the list {l}.  This is
     * equivalent to {nth}.
     *)
+
+   val init : 'a t -> 'a t
+   (**
+    * Return all the elements of a list except the last one.  Raises
+    * {Empty} if the list is empty.
+    *)
+
+   val split : 'a t * int -> 'a t * 'a t
+   (**
+    * {split (l, i)} returns a pair f the first {i} and last {length l -
+    * i} elements of the list {l}.  Raises {Subscript} if {i < 0 orelse
+    * length l < i}.  Specifically, {split (l, n) = (take (l, n), drop (l,
+    * n))}.
+    *)
+
+   (** == Transformations == *)
+
+   val intersperse : 'a -> 'a t -> 'a t
+   (**
+    * {intersperse d l} forms the list {[sub (l, 0), d, sub (l, 1), d,
+    * ..., d, sub (l, n-1)]} where {n = length l}.
+    *)
+
+   val transpose : 'a t t -> 'a t t
+   (** Transposes the rows and columns of its argument. *)
+
+   val index : 'a t -> (int * 'a) t
+   (**
+    * {index l} returns the list {[(0, sub (l, 0)), (1, sub (l, 1)), ...,
+    * (n-1, sub (l, n-1))]} where {n = length l} is the length of the
+    * given list.
+    *)
+
+   (** == Stack == *)
+
+   val push : 'a t ref * 'a -> unit
+   val pop : 'a t ref -> 'a option
+
+   (** == HOFs == *)
+
+   val foldl1 : ('a * 'a -> 'a) -> 'a t -> 'a
+   val foldr1 : ('a * 'a -> 'a) -> 'a t -> 'a
+
+   val appr : ('a -> unit) -> 'a t -> unit
+   (** {appr f l} applies {f} to the elements of {l}, from right to left. *)
+
+   val concatMap : ('a -> 'b t) -> 'a t -> 'b t
+
+   (** == Indexed HOFs == *)
+
+   val appi : (int * 'a -> unit) -> 'a t -> unit
+   val appri : (int * 'a -> unit) -> 'a t -> unit
+   val concatMapi : (int * 'a -> 'b t) -> 'a t -> 'b t
+   val mapi : (int * 'a -> 'b) -> 'a t -> 'b t
+   val mapiPartial : (int * 'a -> 'b option) -> 'a t -> 'b t
+   val foldli : (int * 'a * 'b -> 'b) -> 'b -> 'a t -> 'b
+   val foldri : (int * 'a * 'b -> 'b) -> 'b -> 'a t -> 'b
+   val alli : (int * 'a -> bool) -> 'a t -> bool
+   val existsi : (int * 'a -> bool) -> 'a t -> bool
+   val findi : (int * 'a -> bool) -> 'a t -> (int * 'a) option
+
+   (** == Set Operations == *)
+
+   val contains : ''a t -> ''a -> bool
+
+   (** == Equality == *)
+
+   val equal : ('a * 'b -> bool) -> 'a t * 'b t -> bool
 end




More information about the MLton-commit mailing list