| 1 | -- The Compiler Toolkit: difference lists |
| 2 | -- |
| 3 | -- Author : Manuel M. T. Chakravarty |
| 4 | -- Created: 24 February 95 |
| 5 | -- |
| 6 | -- Copyright (c) [1995..2000] Manuel M. T. Chakravarty |
| 7 | -- |
| 8 | -- This library is free software; you can redistribute it and/or |
| 9 | -- modify it under the terms of the GNU Library General Public |
| 10 | -- License as published by the Free Software Foundation; either |
| 11 | -- version 2 of the License, or (at your option) any later version. |
| 12 | -- |
| 13 | -- This library is distributed in the hope that it will be useful, |
| 14 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | -- Library General Public License for more details. |
| 17 | -- |
| 18 | --- DESCRIPTION --------------------------------------------------------------- |
| 19 | -- |
| 20 | -- This module provides the functional equivalent of the difference lists |
| 21 | -- from logic programming. They provide an O(1) append. |
| 22 | -- |
| 23 | --- DOCU ---------------------------------------------------------------------- |
| 24 | -- |
| 25 | -- language: Haskell 98 |
| 26 | -- |
| 27 | --- TODO ---------------------------------------------------------------------- |
| 28 | -- |
| 29 | |
| 30 | module DLists (DList, openDL, zeroDL, unitDL, snocDL, joinDL, closeDL) |
| 31 | where |
| 32 | |
| 33 | -- a difference list is a function that given a list returns the original |
| 34 | -- contents of the difference list prepended at the given list (EXPORTED) |
| 35 | -- |
| 36 | type DList a = [a] -> [a] |
| 37 | |
| 38 | -- open a list for use as a difference list (EXPORTED) |
| 39 | -- |
| 40 | openDL :: [a] -> DList a |
| 41 | openDL = (++) |
| 42 | |
| 43 | -- create a difference list containing no elements (EXPORTED) |
| 44 | -- |
| 45 | zeroDL :: DList a |
| 46 | zeroDL = id |
| 47 | |
| 48 | -- create difference list with given single element (EXPORTED) |
| 49 | -- |
| 50 | unitDL :: a -> DList a |
| 51 | unitDL = (:) |
| 52 | |
| 53 | -- append a single element at a difference list (EXPORTED) |
| 54 | -- |
| 55 | snocDL :: DList a -> a -> DList a |
| 56 | snocDL dl x = \l -> dl (x:l) |
| 57 | |
| 58 | -- appending difference lists (EXPORTED) |
| 59 | -- |
| 60 | joinDL :: DList a -> DList a -> DList a |
| 61 | joinDL = (.) |
| 62 | |
| 63 | -- closing a difference list into a normal list (EXPORTED) |
| 64 | -- |
| 65 | closeDL :: DList a -> [a] |
| 66 | closeDL = ($[]) |