Data.DList

dlist-0.5: Differences lists

Portability | portable (Haskell 98)
Stability | experimental
Maintainer | dons@cse.unsw.edu.au

Data.DList

Contents

- Construction
- Basic functions
- MonadPlus

Description

Difference lists: a data structure for O(1) append on lists.

Synopsis

- newtype DList a = DL {
  - unDL :: [a] -> [a]
  }
- fromList :: [a] -> DList a
- toList :: DList a -> [a]
- empty :: DList a
- singleton :: a -> DList a
- cons :: a -> DList a -> DList a
- snoc :: DList a -> a -> DList a
- append :: DList a -> DList a -> DList a
- concat :: [DList a] -> DList a
- replicate :: Int -> a -> DList a
- list :: b -> (a -> DList a -> b) -> DList a -> b
- head :: DList a -> a
- tail :: DList a -> DList a
- unfoldr :: (b -> Maybe (a, b)) -> b -> DList a
- foldr :: (a -> b -> b) -> b -> DList a -> b
- map :: (a -> b) -> DList a -> DList b
- maybeReturn :: MonadPlus m => Maybe a -> m a

# Documentation

newtype DList a

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 and snoc operations on lists, making it very useful for append-heavy uses, such as logging and pretty printing.

For example, using DList as the state type when printing a tree with the Writer monad

     import Control.Monad.Writer
     import Data.DList

     data Tree a = Leaf a | Branch (Tree a) (Tree a)

     flatten_writer :: Tree x -> DList x
     flatten_writer = snd . runWriter . flatten
         where
           flatten (Leaf x)     = tell (singleton x)
           flatten (Branch x y) = flatten x >> flatten y

Constructors

DL |
Fields unDL :: [a] -> [a]

Instances

Monad DList |
Functor DList |
MonadPlus DList |
Applicative DList |
Monoid (DList a) |

# Construction

fromList :: [a] -> DList a

Converting a normal list to a dlist

toList :: DList a -> [a]

Converting a dlist back to a normal list

# Basic functions

empty :: DList a

Create a difference list containing no elements

singleton :: a -> DList a

Create difference list with given single element

cons :: a -> DList a -> DList a

O(1), Prepend a single element to a difference list

snoc :: DList a -> a -> DList a

O(1), Append a single element at a difference list

append :: DList a -> DList a -> DList a

O(1), Appending difference lists

concat :: [DList a] -> DList a

O(spine), Concatenate difference lists

replicate :: Int -> a -> DList a

O(n), Create a difference list of the given number of elements

list :: b -> (a -> DList a -> b) -> DList a -> b

O(length dl), List elimination, head, tail.

head :: DList a -> a

Return the head of the list

tail :: DList a -> DList a

Return the tail of the list

unfoldr :: (b -> Maybe (a, b)) -> b -> DList a

Unfoldr for difference lists

foldr :: (a -> b -> b) -> b -> DList a -> b

Foldr over difference lists

map :: (a -> b) -> DList a -> DList b

Map over difference lists.

# MonadPlus

maybeReturn :: MonadPlus m => Maybe a -> m a