annotate src/list_util.sml @ 32:0ff8c2728634

Matching values in signatures
author Adam Chlipala <adamc@hcoop.net>
date Thu, 12 Jun 2008 17:35:51 -0400
parents 1c91c5e6840f
children 44b5405e74c7
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@26 48 fun foldlMap f s =
adamc@26 49 let
adamc@26 50 fun fm (ls', s) ls =
adamc@26 51 case ls of
adamc@26 52 nil => (rev ls', s)
adamc@26 53 | h :: t =>
adamc@26 54 let
adamc@26 55 val (h', s') = f (h, s)
adamc@26 56 in
adamc@26 57 fm (h' :: ls', s') t
adamc@26 58 end
adamc@26 59 in
adamc@26 60 fm ([], s)
adamc@26 61 end
adamc@26 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