toolshed- Utilities used by other packages.

Safe HaskellNone




Dr. Alistair Ward
Miscellaneous polymorphic list-operations.




type ChunkLength = Int Source

The length of the chunks into which a list is split.

type Matches a = a -> a -> Bool Source

The type of function required by findConvergenceBy, permutationsBy.


chunk Source


:: ChunkLength 
-> [a]

The polymorphic input list to be chunked.

-> [[a]] 
  • Splits a list into chunks of the specified length.
  • The last chunk will be shorter, if the chunk-length isn't an aliquot part of the input list-length.
  • If the chunk-length is zero, the resulting list will be an infinite sequence of null lists.
  • CAVEAT: a similar function is available in the module Data.List.Split, though this one checks for (chunkLength < 0).

excise Source


:: Int

The index.

-> [a]

The polymorphic input list.

-> [a]

The same list, with the indexed element removed.

Remove the single indexed element from the list.

equalityBy :: Eq b => (a -> b) -> Matches a Source

A convenient way to compose the Matches-function required by findConvergenceBy & permutationsBy.

findConvergence :: Eq a => [a] -> a Source

A specific instance of findConvergenceBy.

findConvergenceBy :: Matches a -> [a] -> a Source

Take the first element from the (potentially infinite) list, which matches the subsequent element, according to the specified function.

linearise :: [(a, a)] -> [a] Source

Converts a list of Pairs, into a narrower list.

merge :: Ord a => [a] -> [a] -> [a] Source

A specific instance of mergeBy.

mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a] Source

  • Merge two sorted lists, according to the specified order, to product a single sorted list.
  • The merge-process is stable, in that where items from each list are equal, they remain in the original order.
  • CAVEAT: duplicates are preserved.

nub' :: Ord a => [a] -> [a] Source

  • A strict version of nub with better time-complexity.
  • CAVEAT: the specified list must be finite, since the entire set is constructed before streaming to a list.
  • CAVEAT: it sorts the output as a side-effect, & consequently it requires a type which implements Ord.

permutations :: [[a]] -> [[a]] Source

  • The list of all permutations, generated by selecting any one datum from each sub-list in turn, from the specified list of lists.
  • A specific instance of permutationsBy, in which no filtering of subsequent lists is performed after each item is selected.
  • NB: differs from permutations, which selects items from a single input list.

permutationsBy :: Matches a -> [[a]] -> [[a]] Source

  • The list of all permutations, generated by selecting any one datum from each sub-list in turn, from the specified list of lists.
  • As each item is selected, the remaining lists are filtered according to the specified Matches-function.
  • Thus /= could be used to select a different item from each list.

takeUntil Source


:: (a -> Bool)

Predicate, used to determine the last item taken.

-> [a]

The polymorphic input list.

-> [a] 
  • Take until the specified predicate is satisfied; including the item which satisfied it.
  • NB: takeWhile (not . test) would return one fewer item.

showListWith Source


:: (Show token, Show element) 
=> (token, token, token)

(Start-delimiter, separator, end-delimiter)

-> [element]

The polymorphic list to show.

-> ShowS 

Show a list, delimited by the specified tokens.