[英]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, head
和tail
都是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.