annotate src/list_util.sml @ 847:0f7e2cca6d9b

<dyn> inside <table>; fix Specialize bug with datatype decls generating other mutually-recursive datatype decls
author Adam Chlipala <adamc@hcoop.net>
date Sat, 13 Jun 2009 14:29:36 -0400
parents b2413e4dd109
children e571fb150a9f
rev   line source
adamc@5 1 (* Copyright (c) 2008, Adam Chlipala
adamc@5 2 * All rights reserved.
adamc@5 3 *
adamc@5 4 * Redistribution and use in source and binary forms, with or without
adamc@5 5 * modification, are permitted provided that the following conditions are met:
adamc@5 6 *
adamc@5 7 * - Redistributions of source code must retain the above copyright notice,
adamc@5 8 * this list of conditions and the following disclaimer.
adamc@5 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@5 10 * this list of conditions and the following disclaimer in the documentation
adamc@5 11 * and/or other materials provided with the distribution.
adamc@5 12 * - The names of contributors may not be used to endorse or promote products
adamc@5 13 * derived from this software without specific prior written permission.
adamc@5 14 *
adamc@5 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@5 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@5 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@5 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@5 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@5 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@5 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@5 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@5 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@5 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@5 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@5 26 *)
adamc@5 27
adamc@5 28 structure ListUtil :> LIST_UTIL = struct
adamc@5 29
adamc@6 30 structure S = Search
adamc@6 31
adamc@110 32 fun mapConcat f =
adamc@110 33 let
adamc@110 34 fun mc acc ls =
adamc@110 35 case ls of
adamc@110 36 [] => rev acc
adamc@110 37 | h :: t => mc (List.revAppend (f h, acc)) t
adamc@110 38 in
adamc@110 39 mc []
adamc@110 40 end
adamc@110 41
adamc@6 42 fun mapfold f =
adamc@6 43 let
adamc@6 44 fun mf ls s =
adamc@6 45 case ls of
adamc@6 46 nil => S.Continue (nil, s)
adamc@6 47 | h :: t =>
adamc@6 48 case f h s of
adamc@6 49 S.Return x => S.Return x
adamc@6 50 | S.Continue (h', s) =>
adamc@6 51 case mf t s of
adamc@6 52 S.Return x => S.Return x
adamc@6 53 | S.Continue (t', s) => S.Continue (h' :: t', s)
adamc@6 54 in
adamc@6 55 mf
adamc@6 56 end
adamc@6 57
adamc@34 58 fun mapfoldB f =
adamc@34 59 let
adamc@34 60 fun mf ctx ls s =
adamc@34 61 case ls of
adamc@34 62 nil => S.Continue (nil, s)
adamc@34 63 | h :: t =>
adamc@34 64 let
adamc@34 65 val (ctx, r) = f (ctx, h)
adamc@34 66 in
adamc@34 67 case r s of
adamc@34 68 S.Return x => S.Return x
adamc@34 69 | S.Continue (h', s) =>
adamc@34 70 case mf ctx t s of
adamc@34 71 S.Return x => S.Return x
adamc@34 72 | S.Continue (t', s) => S.Continue (h' :: t', s)
adamc@34 73 end
adamc@34 74 in
adamc@34 75 mf
adamc@34 76 end
adamc@34 77
adamc@26 78 fun foldlMap f s =
adamc@26 79 let
adamc@26 80 fun fm (ls', s) ls =
adamc@26 81 case ls of
adamc@26 82 nil => (rev ls', s)
adamc@26 83 | h :: t =>
adamc@26 84 let
adamc@26 85 val (h', s') = f (h, s)
adamc@26 86 in
adamc@26 87 fm (h' :: ls', s') t
adamc@26 88 end
adamc@26 89 in
adamc@26 90 fm ([], s)
adamc@26 91 end
adamc@26 92
adamc@39 93 fun foldlMapConcat f s =
adamc@39 94 let
adamc@39 95 fun fm (ls', s) ls =
adamc@39 96 case ls of
adamc@39 97 nil => (rev ls', s)
adamc@39 98 | h :: t =>
adamc@39 99 let
adamc@39 100 val (h', s') = f (h, s)
adamc@39 101 in
adamc@39 102 fm (List.revAppend (h', ls'), s') t
adamc@39 103 end
adamc@39 104 in
adamc@39 105 fm ([], s)
adamc@39 106 end
adamc@39 107
adamc@39 108 fun foldlMapPartial f s =
adamc@39 109 let
adamc@39 110 fun fm (ls', s) ls =
adamc@39 111 case ls of
adamc@39 112 nil => (rev ls', s)
adamc@39 113 | h :: t =>
adamc@39 114 let
adamc@39 115 val (h', s') = f (h, s)
adamc@39 116 val ls' = case h' of
adamc@39 117 NONE => ls'
adamc@39 118 | SOME h' => h' :: ls'
adamc@39 119 in
adamc@39 120 fm (ls', s') t
adamc@39 121 end
adamc@39 122 in
adamc@39 123 fm ([], s)
adamc@39 124 end
adamc@39 125
adamc@847 126 fun foldlMapAbort f s =
adamc@847 127 let
adamc@847 128 fun fm (ls', s) ls =
adamc@847 129 case ls of
adamc@847 130 nil => SOME (rev ls', s)
adamc@847 131 | h :: t =>
adamc@847 132 case f (h, s) of
adamc@847 133 NONE => NONE
adamc@847 134 | SOME (h', s') => fm (h' :: ls', s') t
adamc@847 135 in
adamc@847 136 fm ([], s)
adamc@847 137 end
adamc@847 138
adamc@23 139 fun search f =
adamc@23 140 let
adamc@23 141 fun s ls =
adamc@23 142 case ls of
adamc@23 143 [] => NONE
adamc@23 144 | h :: t =>
adamc@23 145 case f h of
adamc@23 146 NONE => s t
adamc@23 147 | v => v
adamc@23 148 in
adamc@23 149 s
adamc@23 150 end
adamc@23 151
adamc@839 152 fun searchi f =
adamc@839 153 let
adamc@839 154 fun s n ls =
adamc@839 155 case ls of
adamc@839 156 [] => NONE
adamc@839 157 | h :: t =>
adamc@839 158 case f (n, h) of
adamc@839 159 NONE => s (n + 1) t
adamc@839 160 | v => v
adamc@839 161 in
adamc@839 162 s 0
adamc@839 163 end
adamc@839 164
adamc@120 165 fun mapi f =
adamc@120 166 let
adamc@120 167 fun m i acc ls =
adamc@120 168 case ls of
adamc@120 169 [] => rev acc
adamc@120 170 | h :: t => m (i + 1) (f (i, h) :: acc) t
adamc@120 171 in
adamc@120 172 m 0 []
adamc@120 173 end
adamc@120 174
adamc@792 175 fun appi f =
adamc@792 176 let
adamc@792 177 fun m i ls =
adamc@792 178 case ls of
adamc@792 179 [] => ()
adamc@792 180 | h :: t => (f (i, h); m (i + 1) t)
adamc@792 181 in
adamc@792 182 m 0
adamc@792 183 end
adamc@792 184
adamc@191 185 fun foldli f =
adamc@191 186 let
adamc@191 187 fun m i acc ls =
adamc@191 188 case ls of
adamc@191 189 [] => acc
adamc@191 190 | h :: t => m (i + 1) (f (i, h, acc)) t
adamc@191 191 in
adamc@191 192 m 0
adamc@191 193 end
adamc@191 194
adamc@275 195 fun foldri f i ls =
adamc@275 196 let
adamc@275 197 val len = length ls
adamc@275 198 in
adamc@275 199 foldli (fn (n, x, s) => f (len - n - 1, x, s)) i (rev ls)
adamc@275 200 end
adamc@275 201
adamc@313 202 fun foldliMap f s =
adamc@313 203 let
adamc@313 204 fun fm (n, ls', s) ls =
adamc@313 205 case ls of
adamc@313 206 nil => (rev ls', s)
adamc@313 207 | h :: t =>
adamc@313 208 let
adamc@313 209 val (h', s') = f (n, h, s)
adamc@313 210 in
adamc@313 211 fm (n + 1, h' :: ls', s') t
adamc@313 212 end
adamc@313 213 in
adamc@313 214 fm (0, [], s)
adamc@313 215 end
adamc@313 216
adamc@792 217 fun appn f n =
adamc@792 218 let
adamc@792 219 fun iter m =
adamc@792 220 if m >= n then
adamc@792 221 ()
adamc@792 222 else
adamc@792 223 (f m;
adamc@792 224 iter (m + 1))
adamc@792 225 in
adamc@792 226 iter 0
adamc@792 227 end
adamc@792 228
adamc@5 229 end