annotate src/list_util.sml @ 1579:e1f4ac9ca34b

Note a parsing restriction in the manual
author Adam Chlipala <adam@chlipala.net>
date Fri, 21 Oct 2011 08:36:10 -0400
parents e571fb150a9f
children
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@849 126 fun foldlMapiPartial f s =
adamc@849 127 let
adamc@849 128 fun fm (n, ls', s) ls =
adamc@849 129 case ls of
adamc@849 130 nil => (rev ls', s)
adamc@849 131 | h :: t =>
adamc@849 132 let
adamc@849 133 val (h', s') = f (n, h, s)
adamc@849 134 val ls' = case h' of
adamc@849 135 NONE => ls'
adamc@849 136 | SOME h' => h' :: ls'
adamc@849 137 in
adamc@849 138 fm (n + 1, ls', s') t
adamc@849 139 end
adamc@849 140 in
adamc@849 141 fm (0, [], s)
adamc@849 142 end
adamc@849 143
adamc@847 144 fun foldlMapAbort f s =
adamc@847 145 let
adamc@847 146 fun fm (ls', s) ls =
adamc@847 147 case ls of
adamc@847 148 nil => SOME (rev ls', s)
adamc@847 149 | h :: t =>
adamc@847 150 case f (h, s) of
adamc@847 151 NONE => NONE
adamc@847 152 | SOME (h', s') => fm (h' :: ls', s') t
adamc@847 153 in
adamc@847 154 fm ([], s)
adamc@847 155 end
adamc@847 156
adamc@23 157 fun search f =
adamc@23 158 let
adamc@23 159 fun s ls =
adamc@23 160 case ls of
adamc@23 161 [] => NONE
adamc@23 162 | h :: t =>
adamc@23 163 case f h of
adamc@23 164 NONE => s t
adamc@23 165 | v => v
adamc@23 166 in
adamc@23 167 s
adamc@23 168 end
adamc@23 169
adamc@839 170 fun searchi f =
adamc@839 171 let
adamc@839 172 fun s n ls =
adamc@839 173 case ls of
adamc@839 174 [] => NONE
adamc@839 175 | h :: t =>
adamc@839 176 case f (n, h) of
adamc@839 177 NONE => s (n + 1) t
adamc@839 178 | v => v
adamc@839 179 in
adamc@839 180 s 0
adamc@839 181 end
adamc@839 182
adamc@120 183 fun mapi f =
adamc@120 184 let
adamc@120 185 fun m i acc ls =
adamc@120 186 case ls of
adamc@120 187 [] => rev acc
adamc@120 188 | h :: t => m (i + 1) (f (i, h) :: acc) t
adamc@120 189 in
adamc@120 190 m 0 []
adamc@120 191 end
adamc@120 192
adamc@849 193 fun mapiPartial f =
adamc@849 194 let
adamc@849 195 fun m i acc ls =
adamc@849 196 case ls of
adamc@849 197 [] => rev acc
adamc@849 198 | h :: t =>
adamc@849 199 m (i + 1) (case f (i, h) of
adamc@849 200 NONE => acc
adamc@849 201 | SOME v => v :: acc) t
adamc@849 202 in
adamc@849 203 m 0 []
adamc@849 204 end
adamc@849 205
adamc@792 206 fun appi f =
adamc@792 207 let
adamc@792 208 fun m i ls =
adamc@792 209 case ls of
adamc@792 210 [] => ()
adamc@792 211 | h :: t => (f (i, h); m (i + 1) t)
adamc@792 212 in
adamc@792 213 m 0
adamc@792 214 end
adamc@792 215
adamc@191 216 fun foldli f =
adamc@191 217 let
adamc@191 218 fun m i acc ls =
adamc@191 219 case ls of
adamc@191 220 [] => acc
adamc@191 221 | h :: t => m (i + 1) (f (i, h, acc)) t
adamc@191 222 in
adamc@191 223 m 0
adamc@191 224 end
adamc@191 225
adamc@275 226 fun foldri f i ls =
adamc@275 227 let
adamc@275 228 val len = length ls
adamc@275 229 in
adamc@275 230 foldli (fn (n, x, s) => f (len - n - 1, x, s)) i (rev ls)
adamc@275 231 end
adamc@275 232
adamc@313 233 fun foldliMap f s =
adamc@313 234 let
adamc@313 235 fun fm (n, ls', s) ls =
adamc@313 236 case ls of
adamc@313 237 nil => (rev ls', s)
adamc@313 238 | h :: t =>
adamc@313 239 let
adamc@313 240 val (h', s') = f (n, h, s)
adamc@313 241 in
adamc@313 242 fm (n + 1, h' :: ls', s') t
adamc@313 243 end
adamc@313 244 in
adamc@313 245 fm (0, [], s)
adamc@313 246 end
adamc@313 247
adamc@792 248 fun appn f n =
adamc@792 249 let
adamc@792 250 fun iter m =
adamc@792 251 if m >= n then
adamc@792 252 ()
adamc@792 253 else
adamc@792 254 (f m;
adamc@792 255 iter (m + 1))
adamc@792 256 in
adamc@792 257 iter 0
adamc@792 258 end
adamc@792 259
adamc@5 260 end