Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Haskell doesn't have split function? [closed]

In many languages there's a function that breaks strings into parts using specified delimiter. It's often called split. You can find it in Python, C#, Java, JavaScript. But Haskell while being quite mature still lacks such function in the standard library. I know there's a library called split providing exactly this function. But it's not the same as having this function in the standard library.

So while this function is so handy and usefull that many other languages added it to their standard library its actually intersting why Haskell doesn't have it. What are the arguments behind not having it?

UPD: The question is about base package i.e. Prelude. In other words why does it have words and lines but doesn't have split?

like image 570
Nolan Avatar asked Sep 12 '25 20:09

Nolan


1 Answers

Several Haskell modules implement a split function, in fact it even has several variants that are more generic than the variants in Python, C#, Java, etc.

The split package [Hackage], has several Convenience functions to split a slit of as:

splitOn :: Eq a => [a] -> [a] -> [[a]]
splitOneOf :: Eq a => [a] -> [a] -> [[a]]
splitWhen :: (a -> Bool) -> [a] -> [[a]]

For example:

Prelude Data.List.Split> splitOneOf ",&" "foo,bar&qux"
["foo","bar","qux"]

If you want to perform high performance text processing, you usually use Text over Strings, since these are stored in a more compact way. The text package [Hackage] has a function splitOn :: Text -> Text -> [Text] to split text in a list of Texts. Furthermore you can use split :: (Char -> Bool) -> Text -> [Text] to split based on a condition of the character. For example:

Prelude Data.Text> :set -XOverloadedStrings
Prelude Data.Text> splitOn ", " "foo,bar, qux, bla, , true"
["foo,bar","qux","bla","","true"]
Prelude Data.Text> import Data.Char
Prelude Data.Text Data.Char> split isDigit "foo1bar22true"
["foo","bar","","true"]

A note on the the standard library

The absolute minimum a Haskell standard library should support is defined in Part II: The Haskell 2010 Libraries of the Haskell'10 report. The number of operations on Data.List is quite limited.

Then there is the Data.List library of GHC, but this is, according to @ØrjanJohansen mainly a superset of the functions in the Haskell report, with functions GHC needs itself.

The haskell platform aims to distribute a set of standard packages. split is part of the libraries of the full platform as is Data.Text.

like image 53
Willem Van Onsem Avatar answered Sep 14 '25 10:09

Willem Van Onsem