
Aaron Denney wrote:
On 2006-04-08, Nils Anders Danielsson
wrote: On Fri, 07 Apr 2006, "Spencer Janssen"
wrote: inits xs = [] : (zipWith take [1..] $ map (const xs) xs)
As this version performs much better and will work as a drop in replacement, I suggest that it be included in the hierarchical libraries.
It is not a drop in replacement. The original inits is strict, this one isn't.
The specification of inits (from the Haskell 98 report):
inits :: [a] -> [[a]] inits [] = [[]] inits (x:xs) = [[]] ++ map (x:) (inits xs)
Is that a property many programs depend on? I'd actually call that a bug of the original.
If you know anything that is bad about the definition above, please tell. But so far I haven't found _anything_ the original inits performs better on. I profiled some expressions. And as you can see, the original inits seems to create alot of junk. I did profiling with print ( inits [1..5000]), with which the original version is just a tad slower. But with print ( length (inits [..])) I couldn't even compare the original version to this one. The old had problems to calculate length (inits [1..20000]) while this one easilly did length (inits [1..500000]). The output is in the pdf files. _Note_ that the length is of [1..500000]. I couldn't be bothered to run my computer for some hours to let the original version of inits calculate it. And this version of inits didn't even show up on the graph when calculating [1..20000] in case you wonder why its not there.