Skip to content

Commit

Permalink
Initial import of dlist package
Browse files Browse the repository at this point in the history
darcs-hash:20061211032153-9c5c1-ffa6da3f510681db5293d3cac0a0a4b8a4a3dcbf
  • Loading branch information
donsbot committed Dec 11, 2006
0 parents commit 7464bf4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Data/DList.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-----------------------------------------------------------------------------
-- |
-- Module : Data.DList
-- Copyright : (c) Don Stewart 2006
-- License : BSD-style (see the file LICENSE)
--
-- Maintainer : [email protected]
-- Stability : experimental
-- Portability : portable (Haskell 98)
--
-- Difference lists: a data structure for O(1) append on lists.
--
-----------------------------------------------------------------------------

module Data.DList (

DList, -- abstract

-- * Construction
fromList, -- :: [a] -> DList a
toList, -- :: DList a -> [a]

-- * Basic functions
empty, -- :: DList a
singleton, -- :: a -> DList a
snoc, -- :: DList a -> a -> DList a
append, -- :: DList a -> DList a -> DList a


) where

-- | A difference list is a function that given a list, returns the
-- original contents of the difference list prepended at the given list
--
-- This structure supports /O(1)/ append/snoc operations on lists
--
type DList a = [a] -> [a]

-- | Converting a normal list to a dlist
fromList :: [a] -> DList a
fromList = (++)

-- | Converting a dlist back to a normal list
toList :: DList a -> [a]
toList = ($[])

-- | Create a difference list containing no elements
empty :: DList a
empty = id

-- | Create difference list with given single element
singleton :: a -> DList a
singleton = (:)

-- | /O(1)/, Append a single element at a difference list
snoc :: DList a -> a -> DList a
snoc = (. (:)) . (.)

-- | Appending difference lists
append :: DList a -> DList a -> DList a
append = (.)
3 changes: 3 additions & 0 deletions Setup.lhs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env runhaskell
> import Distribution.Simple
> main = defaultMain
10 changes: 10 additions & 0 deletions dlist.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Name: dlist
Version: 0.1
Description: Differences lists: lists supporting efficient append
License: BSD3
License-file: LICENSE
Author: Don Stewart
Maintainer: <[email protected]>
Build-Depends: base
ghc-options: -O
Exposed-modules: Data.DList

0 comments on commit 7464bf4

Please sign in to comment.