简体   繁体   English

当我们已经有列表时,为什么Haskell需要Data.Sequence?

[英]Why Haskell need Data.Sequence when we already have list?

List is a default data type of Haskell, why we still need Data.Sequence? List是Haskell的默认数据类型,为什么我们仍然需要Data.Sequence? Does Data.Seq mean something like a C style array that could be accessed randomly? Data.Seq是否意味着可以随机访问的C样式数组?

If yes, I would suppose this means Data.Sequence is stored with fixed memory buffer and thus, eager evaluated type. 如果是,我认为这意味着Data.Sequence存储有固定的内存缓冲区,因此,急切的评估类型。 Just a guess, would you help to correct? 只是一个猜测,你会帮忙纠正吗? Thanks. 谢谢。

The list type is a single-linked list. 列表类型是单链接列表。 As such, prepend, head and tail are all O(1). 因此,prepend, headtail都是O(1)。 However, ++ is O(n) in the size of the left-hand list. 但是, ++是左手列表大小的O(n)。

By contrast, Data.Sequence is a balanced tree, so most operations on it are O(log n). 相比之下, Data.Sequence是一个平衡树,因此它上面的大多数操作都是O(log n)。 That's not as fast as O(1), but it's potentially much faster O(n). 这不如O(1)快,但它可能更快O(n)。 In other words, you can join sequences faster than lists, but prepend is slightly slower. 换句话说,您可以比列表更快地连接序列,但前置稍慢。

Other than that, both data structures have quite similar properties; 除此之外,两种数据结构都具有非常相似的特性; they're both lazy, they're both referentially transparent. 他们都懒惰,他们都是公民透明的。 (Sequence has to be finite though.) (序列必须是有限的。)

See also the opening remarks from the documentation for Data.Sequence : 另请参阅Data.Sequence文档中的开头评论:

General purpose finite sequences. 通用有限序列。 Apart from being finite and having strict operations, sequences also differ from lists in supporting a wider variety of operations efficiently. 除了有限且具有严格的操作之外,序列还与有效支持更广泛操作的列表不同。

The underlying algorithm is apparently described here . 这里显然描述了基础算法。 (In particular, includes a nice diagram.) (特别是,包括一个很好的图表。)

If you want arrays , you need to look at Data.Array and/or Data.Vector . 如果需要数组 ,则需要查看Data.Array和/或Data.Vector

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于将数据附加到列表末尾的 Data.Sequence 与 Data.DList - Data.Sequence vs. Data.DList for appending data to the end of the list 当我们已经创建了ArrayList来存储JSONArray时,还需要创建列表吗? - What is need of creating the list when we have already created the ArrayList to store the JSONArray? 为什么我们需要在使用 map 函数时将 list 写在前面,因为 split() 已经返回了一个列表? - Why do we need to write list at front while using map function as split() already returns a list? 为什么当我们为列表列表中的每个列表列出列表的所有元素时,我们需要在 for in 子句中使用特定的顺序? - Why when we list all the elements of a list for every list in a list of lists we need a specific order in the for in clauses? 当我已导入java.util时,为什么必须导入java.util.List。*? - Why do I have to import java.util.List when I already imported java.util.*? 用于列表列表中元组序列的Haskell算法 - Haskell algorithm for sequence of tuples in a list of lists 在Haskell列表理解中组织元组序列 - Organize the sequence of tuple in a Haskell List comprehension 无论如何我们都有一个空列表时如何使用空列表 - How to use empty list when we have an empty list anyways 从已经绘制的数据点制作列表 - Making a list from data points which already have been plotted 为什么Haskell [](列表)不是类型类? - Why is Haskell [] (list) not a type class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM