Split Function in Haskell: How to Use It

Formulas & functions · Updated November 23, 2026

The short answer: Haskell does not have a single built-in split function in the standard Prelude. You get splitting via Data.List.splitOn (from the split package), Data.List.Split for more advanced splitting, or by using words and lines for whitespace and newline splitting. This article explains each option, how to use them, and how to write your own splitter if you need one.

Background: Why No Built-In Split?

Haskell’s standard library (Prelude) is deliberately minimal. It includes words and lines, which split on whitespace and newline characters respectively, but a general-purpose split that takes a delimiter and a list was left out to keep the core library small. The community created the split package to fill this gap. It is maintained by the Haskell community and is widely used, though it is not part of the base library that ships with GHC.

If you are using GHC (the most common Haskell compiler), you may need to install the split package via cabal install split or by adding it to your .cabal file’s build-depends. In a script using runghc, you can add {-# LANGUAGE PackageImports #-} and import it as import qualified Data.List.Split as S.

Using Data.List.Split: The splitOn Function

The most common function is splitOn, which splits a list on every occurrence of a delimiter. It works on any list type, not just strings, as long as the elements are comparable with Eq. The delimiter is a list itself; for strings, that means a substring.

Example:

import Data.List.Split (splitOn)

main :: IO ()
main = print (splitOn "," "apple,banana,cherry")
-- Output: ["apple","banana","cherry"]

For a single character delimiter, you can still use splitOn with a one-character string, but there is also splitOn‘s cousin splitOneOf for splitting on any character from a set. For example, splitOneOf ",;" "a,b;c" gives ["a","b","c"].

Note that splitOn keeps empty elements. Splitting "a,,b" on "," yields ["a","","b"]. If you want to drop empty strings, use filter (not . null) after splitting, or use words which already handles multiple spaces.

Built-In Options: words and lines

The Prelude provides two simple splitters:

  • words :: String -> [String] splits a string on any whitespace (spaces, tabs, newlines) and drops leading/trailing whitespace. It does not keep empty strings. Example: words " a b " gives ["a","b"].
  • lines :: String -> [String] splits on newline characters, keeping empty lines. Example: lines "a\nb\n" gives ["a","b",""].

These are useful for reading text files or command-line input. For splitting on a specific delimiter like a comma or a colon, you need splitOn or a custom function.

How to Choose the Right Splitter

Your choice depends on the delimiter and whether you want empty elements:

  • If you want to split on any whitespace and ignore extra spaces, use words.
  • If you want to split on newlines exactly, use lines.
  • If you want to split on a specific substring (like ", " or "::"), use splitOn from Data.List.Split.
  • If you want to split on any character from a set (like punctuation), use splitOneOf.
  • If you need to split into chunks of a fixed size, use chunksOf from the same package: chunksOf 3 [1..10] gives [[1,2,3],[4,5,6],[7,8,9],[10]].

If you are working with Text (the efficient Unicode text type), you should use Data.Text.splitOn from the text package, which works similarly but on Text values. For ByteString, use Data.ByteString.split (splits on a single byte) or Data.ByteString.Lazy.splitOn.

Writing Your Own Split Function

If you cannot use the split package, you can write a simple recursive splitter. Here is a function that splits on a single character delimiter:

splitOnChar :: Char -> String -> [String]
splitOnChar _ [] = [""]
splitOnChar c s = case break (== c) s of
    (before, []) -> [before]
    (before, _:after) -> before : splitOnChar c after

This uses break, which splits the list at the first occurrence of the predicate. The empty list case returns [""] to match the behavior of splitOn on an empty string. You can test it: splitOnChar ',' "a,b,c" gives ["a","b","c"].

For a more general version that works on any list with an Eq instance, you can adapt the above to take a delimiter list and use isPrefixOf from Data.List.

Common Pitfalls and Misconceptions

One common mistake is expecting splitOn to drop empty strings. It does not; it preserves them. If you want to remove empty strings, you must filter them out. For example:

import Data.List.Split (splitOn)

cleanSplit :: String -> [String]
cleanSplit = filter (not . null) . splitOn ","

Another misconception is that words splits on spaces only. In fact, it splits on any Unicode whitespace character, including tabs and newlines. This is usually what you want, but if you need to split on a literal space only, use splitOn " ".

Also, be aware that splitOn is not lazy in the way you might expect. It will consume the entire list to produce the first element because it needs to find the delimiter. For infinite lists, this can hang. If you need to process an infinite stream, consider using Data.List.Split.splitOn with a custom fold, or use a streaming library.

Performance and Efficiency Considerations

For most uses, splitOn is efficient enough. It runs in O(n*m) worst-case where n is the length of the input and m is the length of the delimiter, but for short delimiters it is effectively O(n). If you are splitting a huge string on a single character, you can use Data.ByteString.split for bytes or Data.Text.splitOn for text, which are optimized and often faster than the list-based version.

If you are splitting on a single character and need maximum speed, you can use a simple recursive function with break as shown above, which avoids the overhead of the split package’s more general machinery. However, the difference is usually negligible unless you are processing megabytes of data.

Frequently Asked Questions

How do I split a string on a comma in Haskell?
Use splitOn "," "a,b,c" from Data.List.Split. You need to import that module. If you cannot use the package, write a simple recursive function using break.

Why doesn’t words split on commas?
Because words is specifically designed for whitespace. It splits on any whitespace character and ignores multiple spaces. For a comma delimiter, you need splitOn or a custom function.

How do I split a string into a list of characters?
In Haskell, a string is already a list of characters, so "abc" is ['a','b','c']. If you need a list of one-character strings, you can use map (:[]) "abc" which gives ["a","b","c"].

Is there a split function in the standard library?
No, the base library does not include a general split. It only has words and lines. The split package is the de facto standard for general splitting.

How do I split on multiple delimiters, like commas or semicolons?
Use splitOneOf ",;" "a,b;c" from Data.List.Split. This splits on any character in the given list. If you need to split on a sequence of characters (like ", "), use splitOn ", ".

How do I split a string into chunks of equal length?
Use chunksOf from Data.List.Split. For example, chunksOf 2 "abcdef" gives ["ab","cd","ef"]. The last chunk may be shorter if the length is not a multiple.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.