问题
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
?
回答1:
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 a
s:
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 String
s, 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 Text
s. 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
.
回答2:
In a nutshell, that's how the ecosystem rolls.
If we take "standard library" literally and look at exactly which modules are specified by the Haskell report, we'll find Haskell's standard library is very minimal -- not even all of base is included. We might reasonably widen our scope a bit, and regard the libraries bundled with GHC as de facto standard. This second group includes some absolutely fundamental stuff, such as the rest of base, bytestring, text, containers, transformers and mtl.
In any case, the broader point is that the notion of a blessed set of core libraries does not play as much as a role in Haskell practice as it does in some other languages. It is entirely possible for a library to play an important role in the ecosystem without being regarded as somehow standard. The split library mentioned by Willem, with its 555 reverse dependencies in Hackage, arguably fits the bill.
来源:https://stackoverflow.com/questions/57109229/why-haskell-doesnt-have-split-function