Community
Modules 3

Data.DList

stable

A difference list is an abstraction representing a list that supports \mathcal{O}(1) append and snoc operations. This module provides the type for a difference list, DList, and a collection of supporting functions for (a) converting to and from lists and (b) operating on DLists efficiently.

Difference List Type

Cons :: a -> [a] -> DList a

A unidirectional pattern synonym for cons. This is implemented with toList.

Nil :: DList a

A unidirectional pattern synonym for empty. This is implemented with toList.

newtype DList a

A difference list is an abstraction representing a list that supports \mathcal{O}(1) append and snoc operations, making it useful for replacing frequent applications of ++ such as logging and pretty printing (esp. if those uses of ++ are left-nested).

Conversion

fromList :: [a] -> DList a

fromList xs is a DList representing the list xs.

fromList obeys the laws:

'toList' . __fromList__ = 'id'
__fromList__ . 'toList' = 'id'

This function is implemented with ++. Repeated uses of fromList are just as inefficient as repeated uses of ++. If you find yourself doing some form of the following (possibly indirectly), you may not be taking advantage of the DList representation and library:

__fromList__ . f . 'toList'

More likely, you will convert from a list, perform some operation on the DList, and convert back to a list:

'toList' . g . __fromList__

toList :: DList a -> [a]

toList xs is the list represented by xs.

toList obeys the laws:

__toList__ . 'fromList' = 'id'
'fromList' . __toList__ = 'id'

Evaluating toList xs may “collapse” the chain of function composition underlying many DList functions (append in particular) used to construct xs. This may affect any efficiency you achieved due to laziness in the construction.

apply :: DList a -> [a] -> [a]

apply xs ys is the list represented by the xs after appending ys to it.

\mathcal{O}(1).

apply obeys the law:

__apply__ xs ys = 'toList' xs '++' ys

Basic Functions

empty :: DList a

empty is a DList with no elements.

empty obeys the law:

'toList' __empty__ = []

singleton :: a -> DList a

singleton x is a DList with the single element x.

singleton obeys the law:

'toList' (__singleton__ x) = [x]

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

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

snoc xs x is a DList with the initial DList xs and the last element x.

\mathcal{O}(1).

snoc obeys the law:

'toList' (__snoc__ xs x) = 'toList' xs '++' [x]

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

append xs ys is a DList obtained from the concatenation of the elements of xs and ys.

\mathcal{O}(1).

append obeys the law:

'toList' (__append__ xs ys) = 'toList' xs '++' 'toList' ys

concat :: [DList a] -> DList a

concat xss is a DList representing the concatenation of all DLists in the list xss.

\mathcal{O}(length xss).

concat obeys the law:

'toList' (__concat__ xss) = 'List.concat' ('List.map' 'toList' xss)

replicate :: Int -> a -> DList a

replicate n x is a DList of length n with x as the value of every element.

\mathcal{O}(n).

replicate obeys the law:

'toList' (__replicate__ n x) = 'List.replicate' n x

head :: DList a -> a

head xs is the first element of xs. If xs is empty, an error is raised.

\mathcal{O}(1).

head obeys the law:

__head__ xs = 'List.head' ('toList' xs)

tail :: DList a -> [a]

tail xs is a list of the elements in xs excluding the first element. If xs is empty, an error is raised.

\mathcal{O}(length (toList xs)).

tail obeys the law:

__tail__ xs = 'List.tail' ('toList' xs)

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

unfoldr f z is the DList constructed from the recursive application of f. The recursion starts with the seed value z and ends when, for some z' : b, f z' == Nothing.

\mathcal{O}(length (unfoldr f z)).

unfoldr obeys the law:

'toList' (__unfoldr__ f z) = 'List.unfoldr' f z

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

foldr f z xs is the right-fold of f over xs.

\mathcal{O}(length (toList xs)).

foldr obeys the law:

__foldr__ f z xs = 'List.foldr' f z ('toList' xs)

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

map f xs is the DList obtained by applying f to each element of xs.

\mathcal{O}(length (toList xs)).

map obeys the law:

'toList' (__map__ f xs) = 'List.map' f ('toList' xs)

intercalate :: DList a -> [DList a] -> DList a

intercalate xs xss is the concatenation of xss after the insertion of xs between every pair of elements.

\mathcal{O}(length xss).

intercalate obeys the law:

'toList' (__intercalate__ xs xss) = 'List.intercalate' ('toList' xs) ('map' 'toList' xss)
Maintainer
sean.leather@gmail.com
Copyright
© 2006-2009 Don Stewart, 2013-2020 Sean Leather
License
BSD-3-Clause
Build record and source files

Community contribution. The service has not verified this documentation. The generator reported 3 diagnostics.

Target: Linux on x86-64. Some inferred types and resolved links are not available.

Generator: aihc-haddock-858b1951-web-1

Metadata snapshot: 5c399ad10bb66f19688f6a371c5eed4c6542a2a6e4873a65871c1260a56b6173

Open the dependency plan · Download the documentation model

Import a package

Enter a package name and an exact version from Hackage. The site contains imported versions only.