adamc@5: (* Copyright (c) 2008, Adam Chlipala adamc@5: * All rights reserved. adamc@5: * adamc@5: * Redistribution and use in source and binary forms, with or without adamc@5: * modification, are permitted provided that the following conditions are met: adamc@5: * adamc@5: * - Redistributions of source code must retain the above copyright notice, adamc@5: * this list of conditions and the following disclaimer. adamc@5: * - Redistributions in binary form must reproduce the above copyright notice, adamc@5: * this list of conditions and the following disclaimer in the documentation adamc@5: * and/or other materials provided with the distribution. adamc@5: * - The names of contributors may not be used to endorse or promote products adamc@5: * derived from this software without specific prior written permission. adamc@5: * adamc@5: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@5: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@5: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@5: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@5: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@5: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@5: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@5: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@5: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@5: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@5: * POSSIBILITY OF SUCH DAMAGE. adamc@5: *) adamc@5: adamc@5: structure ListUtil :> LIST_UTIL = struct adamc@5: adamc@5: fun mapfoldl f i = adamc@5: let adamc@5: fun mf s ls' ls = adamc@5: case ls of adamc@5: nil => (s, rev ls') adamc@5: | h :: t => adamc@5: let adamc@5: val (s, h') = f (h, s) adamc@5: in adamc@5: mf s (h' :: ls') t adamc@5: end adamc@5: in adamc@5: mf i [] adamc@5: end adamc@5: adamc@5: end