English 中文(简体)
怎样?
原标题:How lazy is Haskell s `++`?

我很奇怪,我应如何改进Haskell的例行工作,这种例行工作在地理上很少地进行传说。

import Data.List
swapAt n = f . splitAt n where f (a,b) = b++a
minimumrotation x = minimum $ map (i -> swapAt i x) $ elemIndices (minimum x) x

我想象我应该使用数据。 由于数据,而不是单列。 病媒提供当地业务,可能只是将一些指数用于原始数据。 我是否确实需要双管齐下指数,以避免过份复制?

I m 曲解++如何影响优化。 我想象,它会产生一种 la笑的th,在扼杀达到顶点之前,从来不做 app。 Ergo,a 无论如何,只要能尽早消除这种扼杀,就永远不应在<条码>b上附上。 这是否正确?

最佳回答

<代码>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.

问题回答

提 出

minimumrotation :: Ord a => [a] -> [a]
minimumrotation xs = minimum . take len . map (take len) $ tails (cycle xs)
  where
    len = length xs

我预计,这比你更快,尽管在未插入的<条码>上索引-计算法>、<条码>或<代码>UArray上的索引计算可能仍然更快。 但是,这是否确实是一个瓶颈?





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签