annotate src/list_util.sml @ 35:1dfbd9e3e790

Proper selfification
author Adam Chlipala <adamc@hcoop.net>
date Tue, 17 Jun 2008 17:08:28 -0400
parents 44b5405e74c7
children 02f42e9a1825
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@6 32 fun mapfold f =
adamc@6 33 let
adamc@6 34 fun mf ls s =
adamc@6 35 case ls of
adamc@6 36 nil => S.Continue (nil, s)
adamc@6 37 | h :: t =>
adamc@6 38 case f h s of
adamc@6 39 S.Return x => S.Return x
adamc@6 40 | S.Continue (h', s) =>
adamc@6 41 case mf t s of
adamc@6 42 S.Return x => S.Return x
adamc@6 43 | S.Continue (t', s) => S.Continue (h' :: t', s)
adamc@6 44 in
adamc@6 45 mf
adamc@6 46 end
adamc@6 47
adamc@34 48 fun mapfoldB f =
adamc@34 49 let
adamc@34 50 fun mf ctx ls s =
adamc@34 51 case ls of
adamc@34 52 nil => S.Continue (nil, s)
adamc@34 53 | h :: t =>
adamc@34 54 let
adamc@34 55 val (ctx, r) = f (ctx, h)
adamc@34 56 in
adamc@34 57 case r s of
adamc@34 58 S.Return x => S.Return x
adamc@34 59 | S.Continue (h', s) =>
adamc@34 60 case mf ctx t s of
adamc@34 61 S.Return x => S.Return x
adamc@34 62 | S.Continue (t', s) => S.Continue (h' :: t', s)
adamc@34 63 end
adamc@34 64 in
adamc@34 65 mf
adamc@34 66 end
adamc@34 67
adamc@26 68 fun foldlMap f s =
adamc@26 69 let
adamc@26 70 fun fm (ls', s) ls =
adamc@26 71 case ls of
adamc@26 72 nil => (rev ls', s)
adamc@26 73 | h :: t =>
adamc@26 74 let
adamc@26 75 val (h', s') = f (h, s)
adamc@26 76 in
adamc@26 77 fm (h' :: ls', s') t
adamc@26 78 end
adamc@26 79 in
adamc@26 80 fm ([], s)
adamc@26 81 end
adamc@26 82
adamc@23 83 fun search f =
adamc@23 84 let
adamc@23 85 fun s ls =
adamc@23 86 case ls of
adamc@23 87 [] => NONE
adamc@23 88 | h :: t =>
adamc@23 89 case f h of
adamc@23 90 NONE => s t
adamc@23 91 | v => v
adamc@23 92 in
adamc@23 93 s
adamc@23 94 end
adamc@23 95
adamc@5 96 end