annotate src/list_util.sml @ 501:7ef4b2911b09

Some demo improvements
author Adam Chlipala <adamc@hcoop.net>
date Thu, 20 Nov 2008 11:34:36 -0500
parents e0ed0d4dabc9
children d20d6afc1206
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@23 126 fun search f =
adamc@23 127 let
adamc@23 128 fun s ls =
adamc@23 129 case ls of
adamc@23 130 [] => NONE
adamc@23 131 | h :: t =>
adamc@23 132 case f h of
adamc@23 133 NONE => s t
adamc@23 134 | v => v
adamc@23 135 in
adamc@23 136 s
adamc@23 137 end
adamc@23 138
adamc@120 139 fun mapi f =
adamc@120 140 let
adamc@120 141 fun m i acc ls =
adamc@120 142 case ls of
adamc@120 143 [] => rev acc
adamc@120 144 | h :: t => m (i + 1) (f (i, h) :: acc) t
adamc@120 145 in
adamc@120 146 m 0 []
adamc@120 147 end
adamc@120 148
adamc@191 149 fun foldli f =
adamc@191 150 let
adamc@191 151 fun m i acc ls =
adamc@191 152 case ls of
adamc@191 153 [] => acc
adamc@191 154 | h :: t => m (i + 1) (f (i, h, acc)) t
adamc@191 155 in
adamc@191 156 m 0
adamc@191 157 end
adamc@191 158
adamc@275 159 fun foldri f i ls =
adamc@275 160 let
adamc@275 161 val len = length ls
adamc@275 162 in
adamc@275 163 foldli (fn (n, x, s) => f (len - n - 1, x, s)) i (rev ls)
adamc@275 164 end
adamc@275 165
adamc@313 166 fun foldliMap f s =
adamc@313 167 let
adamc@313 168 fun fm (n, ls', s) ls =
adamc@313 169 case ls of
adamc@313 170 nil => (rev ls', s)
adamc@313 171 | h :: t =>
adamc@313 172 let
adamc@313 173 val (h', s') = f (n, h, s)
adamc@313 174 in
adamc@313 175 fm (n + 1, h' :: ls', s') t
adamc@313 176 end
adamc@313 177 in
adamc@313 178 fm (0, [], s)
adamc@313 179 end
adamc@313 180
adamc@5 181 end