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@5
|
30 fun mapfoldl f i =
|
adamc@5
|
31 let
|
adamc@5
|
32 fun mf s ls' ls =
|
adamc@5
|
33 case ls of
|
adamc@5
|
34 nil => (s, rev ls')
|
adamc@5
|
35 | h :: t =>
|
adamc@5
|
36 let
|
adamc@5
|
37 val (s, h') = f (h, s)
|
adamc@5
|
38 in
|
adamc@5
|
39 mf s (h' :: ls') t
|
adamc@5
|
40 end
|
adamc@5
|
41 in
|
adamc@5
|
42 mf i []
|
adamc@5
|
43 end
|
adamc@5
|
44
|
adamc@6
|
45 structure S = Search
|
adamc@6
|
46
|
adamc@6
|
47 fun mapfold f =
|
adamc@6
|
48 let
|
adamc@6
|
49 fun mf ls s =
|
adamc@6
|
50 case ls of
|
adamc@6
|
51 nil => S.Continue (nil, s)
|
adamc@6
|
52 | h :: t =>
|
adamc@6
|
53 case f h s of
|
adamc@6
|
54 S.Return x => S.Return x
|
adamc@6
|
55 | S.Continue (h', s) =>
|
adamc@6
|
56 case mf t s of
|
adamc@6
|
57 S.Return x => S.Return x
|
adamc@6
|
58 | S.Continue (t', s) => S.Continue (h' :: t', s)
|
adamc@6
|
59 in
|
adamc@6
|
60 mf
|
adamc@6
|
61 end
|
adamc@6
|
62
|
adamc@23
|
63 fun search f =
|
adamc@23
|
64 let
|
adamc@23
|
65 fun s ls =
|
adamc@23
|
66 case ls of
|
adamc@23
|
67 [] => NONE
|
adamc@23
|
68 | h :: t =>
|
adamc@23
|
69 case f h of
|
adamc@23
|
70 NONE => s t
|
adamc@23
|
71 | v => v
|
adamc@23
|
72 in
|
adamc@23
|
73 s
|
adamc@23
|
74 end
|
adamc@23
|
75
|
adamc@5
|
76 end
|