annotate src/list_util.sml @ 26:4ab19c19665f

Closure conversion
author Adam Chlipala <adamc@hcoop.net>
date Tue, 10 Jun 2008 15:56:33 -0400
parents bfa2e9ae4df8
children 1c91c5e6840f
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@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@26 63 fun foldlMap f s =
adamc@26 64 let
adamc@26 65 fun fm (ls', s) ls =
adamc@26 66 case ls of
adamc@26 67 nil => (rev ls', s)
adamc@26 68 | h :: t =>
adamc@26 69 let
adamc@26 70 val (h', s') = f (h, s)
adamc@26 71 in
adamc@26 72 fm (h' :: ls', s') t
adamc@26 73 end
adamc@26 74 in
adamc@26 75 fm ([], s)
adamc@26 76 end
adamc@26 77
adamc@23 78 fun search f =
adamc@23 79 let
adamc@23 80 fun s ls =
adamc@23 81 case ls of
adamc@23 82 [] => NONE
adamc@23 83 | h :: t =>
adamc@23 84 case f h of
adamc@23 85 NONE => s t
adamc@23 86 | v => v
adamc@23 87 in
adamc@23 88 s
adamc@23 89 end
adamc@23 90
adamc@5 91 end