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. Scroll past the Synopsis 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,
splitWhen, sepBy, endBy, splitEvery, 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, skip to the section labeled "Splitting Combinators".

Note that 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 even though it
is not the default.

Note that this is the right inverse of the intercalate function
from Data.List, that is, intercalate x . unintercalate x
== id. It is also the case that unintercalate x
. intercalate x is idempotent. unintercalate 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.)

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.