为什么 Haskell 没有拆分功能? [英] Why Haskell doesn't have split function?

查看:103
本文介绍了为什么 Haskell 没有拆分功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多语言中,有一个函数可以使用指定的分隔符将字符串分成几部分.它通常被称为 split.您可以在 Python、C#、Java、JavaScript 中找到它.但是Haskell虽然已经比较成熟,但是标准库中还缺少这样的功能.我知道有一个名为 split 的库提供了这个功能.但是和标准库里有这个函数不一样.

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.

所以虽然这个函数是如此的方便和有用,以至于许多其他语言将它添加到他们的标准库中,但实际上很有趣的是为什么 Haskell 没有它.没有它的理由是什么?

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:问题是关于 base 包,即 Prelude.换句话说,为什么它有 wordslines 但没有 split?

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?

推荐答案

几个 Haskell 模块实现了 split 函数,实际上它甚至有几个变体比 Python、C#、Java 等中的变体更通用.

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.

split 包[Hackage],有几个 便利功能分割a的一个缝隙:

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]]

例如:

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

如果要进行高性能的文本处理,通常使用 TextString 上,因为它们以更紧凑的方式存储.text 包 [Hackage] 有一个函数 splitOn :: Text ->文本 ->[Text] 拆分 Text 列表中的文本.此外,您可以使用 <代码>split :: (Char -> Bool) ->文本 ->[Text] 根据字符的条件进行拆分.例如:

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"]

关于标准库

Haskell 标准库应该支持的绝对最小值定义在 第二部分:Haskell'10 报告的 Haskell 2010 图书馆.Data.List<上的操作次数/a> 非常有限.

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.

然后是 Data.List GHC 库,但这是根据 @ØrjanJohansena> 主要是 Haskell 报告中函数的超集,具有 GHC 本身需要的函数.

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.

haskell 平台旨在分发一组标准包.splitData.Text 一样是 完整平台 库的一部分.

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.

这篇关于为什么 Haskell 没有拆分功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆