为什么列表插入、修改和删除功能不在预加载模块中?

huangapple go评论68阅读模式
英文:

Why are list insert, modify, remove function are not in prelude?

问题

Haskell社区为什么决定不在Prelude中包含这些常见用例的原因是什么?

英文:

Please consider the following functions (list manipulation according to index):

modify items index value = take index items ++ value : drop (index + 1) items
insert index element items = take index items ++ element : drop index items
remove index items = take index items ++ drop (1 + index) items

What was the reason that Haskell people decided not to include such common use cases in the Prelude?

Thanks in advance.

答案1

得分: 2

以下是翻译好的内容:

"预备篇旨在包括一组相对精简的基本定义。以下是来自2010年Haskell报告的相关段落:

还有许多预定义的库模块,提供不太常用的函数和类型。[...] 将库与Prelude分开的优点是减少了Prelude的大小和复杂性,使其更容易吸收,并增加了程序员可用的有用名称的空间。

因此,除了基本要求之外的用例,包括相当常见的用例,通常都会由Prelude之外的模块或base之外的库来处理。 (有关相关问答,请参阅为什么Haskell没有分割函数?。)

在您的具体示例中,通过索引修改、插入或删除列表元素并未被认为足够常见,不值得包含在Prelude中,甚至不值得包含在Data.List中。其中一个关键原因是Haskell列表更适用于顺序流式访问,而不是随机访问。在需要大量随机访问的情况下,预计用户将切换到更适合手头任务的数据结构,例如序列映射向量。它们很容易提供一些或所有具有足够性能特性的函数的类似物。例如,Data.Sequence包括updateinsertAtdeleteAt。"

英文:

The Prelude is meant to be comprised of a relatively lean set of basic definitions. Below is a relevant passage from the Haskell 2010 Report:

> There are also many predefined library modules, which provide less frequently used functions and types. [...] Separating libraries from the Prelude has the advantage of reducing the size and complexity of the Prelude, allowing it to be more easily assimilated, and increasing the space of useful names available to the programmer.

That being so, use cases beyond the bare essentials, including pretty common ones, will typically be covered by modules other than the Prelude, or libraries other than base. (For a related Q&A, see also Why Haskell doesn't have split function?.)

In your concrete example, modifying, inserting or removing list elements by index have not been deemed common enough to merit inclusion in the Prelude, or even in Data.List. A key reason for that is Haskell lists being geared for sequential, streaming access, rather than random access. In situations which require lots of random access, it is expected that users will switch to data structures better suited to the task at hand, such as sequences, maps or vectors. They readily offer analogues for some or all of your functions with adequate performance characteristics. For instance, Data.Sequence includes update, insertAt and deleteAt.

答案2

得分: 0

在Haskell中,一切都是不可变的。提供“标准”函数,其语义内涵(从名称中)可能暗示可变性和这种思维方式是没有意义的。这是误导性和错误的,不仅因为传递的实体不会发生实际修改,并且总是从函数中返回新创建的实体。

另一方面,根据需要,基于语言中更通用的函数,可以轻松创建无限数量的“更具体”的函数。

英文:

Everything in Haskell is immutable. It does not make sense to provide „standard“ functions which semantical connotation (from names) might imply mutability and this way of thinking. It is misleading and wrong, and not only because no actual modification of passed entities happens and always newly created are returned from the functions.

On the other side infinite number of such „more specific“ functions is elementary created based on more generic ones from the language as needed.

huangapple
  • 本文由 发表于 2023年4月17日 14:00:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定