HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulerebase-1.21.2Haskell2010

Rebase.Data.DList

  • 1 type
  • 16 values
  • Packagerebase-1.21.2
  • Exports19
  • LanguageHaskell2010
  • LicenceMIT
  • SourceInternal.hs
newtypenewtype 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).

Instances18Monad, Functor, MonadFail, Applicative, Foldable, Traversable, …
patternpattern Nil :: DList a
#

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

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

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

valuetoList :: 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.

valuefoldr :: (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 = foldr f z (toList xs)
valuereplicate :: 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) = replicate n x
valuehead :: 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 = head (toList xs)
valueapply :: 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
valueunfoldr :: (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) = unfoldr f z
valuesingleton :: a -> DList a
#

singleton x is a DList with the single element x.

singleton obeys the law:

toList (singleton x) = [x]
valuetail :: 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 = tail (toList xs)
valuefromList :: [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