<代码>xs ++ ys在所有清单小组中增加一些间接费用,从,但一旦到<代码>xs<>/code>的末尾,便可免费使用<代码>ys。
查阅<代码>(++)的定义有助于了解为什么:
[] ++ ys = ys
(x:xs) ++ ys = x : (xs ++ ys)
i.e. 它必须“re-build”,其结果为:。 本条非常有助于理解如何以这种方式解释有关zy的法典。
关键是要做到的是, app总只做一次;通过所有<条码>xs逐步建立新的链接清单,然后将<条码>s<>s <代码>。
因此,你不必担心达到<代码>b的末尾,并突然承担“申请”代码<<>a的一次性费用;费用分散在<编码>b的所有内容上。
Vectors are a different matter entirely; they re strict in their structure, so even examining just the first element of xs V.++ ys
incurs the entire overhead of allocating a new vector and copying xs
and ys
to it — just like in a strict language. The same applies to mutable vectors (except that the cost is incurred when you perform the operation, rather than when you force the resulting vector), although I think you d have to write your own append operation with those anyway. You could represent a bunch of appended (immutable) vectors as [Vector a]
or similar if this is a problem for you, but that just moves the overhead to when you flattening it back into a single Vector, and it sounds like you re more interested in mutable vectors.