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@839
|
139 fun searchi f =
|
adamc@839
|
140 let
|
adamc@839
|
141 fun s n ls =
|
adamc@839
|
142 case ls of
|
adamc@839
|
143 [] => NONE
|
adamc@839
|
144 | h :: t =>
|
adamc@839
|
145 case f (n, h) of
|
adamc@839
|
146 NONE => s (n + 1) t
|
adamc@839
|
147 | v => v
|
adamc@839
|
148 in
|
adamc@839
|
149 s 0
|
adamc@839
|
150 end
|
adamc@839
|
151
|
adamc@120
|
152 fun mapi f =
|
adamc@120
|
153 let
|
adamc@120
|
154 fun m i acc ls =
|
adamc@120
|
155 case ls of
|
adamc@120
|
156 [] => rev acc
|
adamc@120
|
157 | h :: t => m (i + 1) (f (i, h) :: acc) t
|
adamc@120
|
158 in
|
adamc@120
|
159 m 0 []
|
adamc@120
|
160 end
|
adamc@120
|
161
|
adamc@792
|
162 fun appi f =
|
adamc@792
|
163 let
|
adamc@792
|
164 fun m i ls =
|
adamc@792
|
165 case ls of
|
adamc@792
|
166 [] => ()
|
adamc@792
|
167 | h :: t => (f (i, h); m (i + 1) t)
|
adamc@792
|
168 in
|
adamc@792
|
169 m 0
|
adamc@792
|
170 end
|
adamc@792
|
171
|
adamc@191
|
172 fun foldli f =
|
adamc@191
|
173 let
|
adamc@191
|
174 fun m i acc ls =
|
adamc@191
|
175 case ls of
|
adamc@191
|
176 [] => acc
|
adamc@191
|
177 | h :: t => m (i + 1) (f (i, h, acc)) t
|
adamc@191
|
178 in
|
adamc@191
|
179 m 0
|
adamc@191
|
180 end
|
adamc@191
|
181
|
adamc@275
|
182 fun foldri f i ls =
|
adamc@275
|
183 let
|
adamc@275
|
184 val len = length ls
|
adamc@275
|
185 in
|
adamc@275
|
186 foldli (fn (n, x, s) => f (len - n - 1, x, s)) i (rev ls)
|
adamc@275
|
187 end
|
adamc@275
|
188
|
adamc@313
|
189 fun foldliMap f s =
|
adamc@313
|
190 let
|
adamc@313
|
191 fun fm (n, ls', s) ls =
|
adamc@313
|
192 case ls of
|
adamc@313
|
193 nil => (rev ls', s)
|
adamc@313
|
194 | h :: t =>
|
adamc@313
|
195 let
|
adamc@313
|
196 val (h', s') = f (n, h, s)
|
adamc@313
|
197 in
|
adamc@313
|
198 fm (n + 1, h' :: ls', s') t
|
adamc@313
|
199 end
|
adamc@313
|
200 in
|
adamc@313
|
201 fm (0, [], s)
|
adamc@313
|
202 end
|
adamc@313
|
203
|
adamc@792
|
204 fun appn f n =
|
adamc@792
|
205 let
|
adamc@792
|
206 fun iter m =
|
adamc@792
|
207 if m >= n then
|
adamc@792
|
208 ()
|
adamc@792
|
209 else
|
adamc@792
|
210 (f m;
|
adamc@792
|
211 iter (m + 1))
|
adamc@792
|
212 in
|
adamc@792
|
213 iter 0
|
adamc@792
|
214 end
|
adamc@792
|
215
|
adamc@5
|
216 end
|