The Data.List.Split module contains a wide range of strategies
for splitting lists with respect to some sort of delimiter, mostly
implemented through a unified combinator interface. The goal is to
be flexible yet simple. See below for usage, examples, and
detailed documentation of all exported functions. If you want to
learn about the implementation, see Data.List.Split.Internals.

Getting started

To get started, you should take a look at the functions splitOn,
splitOneOf, splitWhen, endBy, chunksOf, splitPlaces,
and other functions listed in the next two sections. These
functions implement various common splitting operations, and one of
them will probably do the job 90% of the time. For example:

If you want more flexibility, however, you can use the combinator
library in terms of which these functions are defined. For more
information, see the section labeled "Splitting Combinators".

The goal of this library is to be flexible yet simple. It does not
implement any particularly sophisticated list-splitting methods,
nor is it tuned for speed. If you find yourself wanting something
more complicated or optimized, it probably means you should use a
real parsing or regular expression library.

Convenience functions

These functions implement some common splitting strategies. Note
that all of the functions in this section drop delimiters from the
final output, since that is a more common use case. If you wish to
keep the delimiters somehow, see the "Splitting Combinators"
section.

splitOn x . intercalate x is the identity on
certain lists, but it is tricky to state the precise conditions
under which this holds. (For example, it is not enough to say
that x does not occur in any elements of the input list.
Working out why is left as an exercise for the reader.)

Other splitting methods

chunksOf n splits a list into length-n pieces. The last
piece will be shorter if n does not evenly divide the length of
the list. If n <= 0, chunksOf n l returns an infinite list
of empty lists. For example:

Note that chunksOf n [] is [], not [[]]. This is
intentional, and is consistent with a recursive definition of
chunksOf; it satisfies the property that

If the input list is longer than the total of the given lengths,
then the remaining elements are dropped. If the list is shorter
than the total of the given lengths, then the result may contain
fewer chunks than requested, and the last chunk may be shorter
than requested.

Split a list into chunks of the given lengths. Unlike
splitPlaces, the output list will always be the same length as
the first input argument. If the input list is longer than the
total of the given lengths, then the remaining elements are
dropped. If the list is shorter than the total of the given
lengths, then the last several chunks will be shorter than
requested or empty. For example:

A useful recursion pattern for processing a list to produce a new
list, often used for "chopping" up the input list. Typically
chop is called with some function that will consume an initial
prefix of the list and produce a value and the rest of the list.

For example, many common Prelude functions can be implemented in
terms of chop:

Splitting combinators

The core of the library is the Splitter type, which represents a
particular list-splitting strategy. All of the combinators revolve
around constructing or transforming Splitter objects; once a
suitable Splitter has been created, it can be run with the
split function. For example:

The defaultSplitter strategy with any delimiter gives a
maximally information-preserving splitting strategy, in the sense
that (a) taking the concat of the output yields the original
list, and (b) given only the output list, we can reconstruct a
Splitter which would produce the same output list again given
the original input list. This default strategy can be overridden
to allow discarding various sorts of information.