inits

Tim Toorop timtoorop at quicknet.nl
Sat Apr 8 14:52:35 EDT 2006


Aaron Denney wrote:
> On 2006-04-08, Nils Anders Danielsson <nad at cs.chalmers.se> wrote:
>   
>> On Fri, 07 Apr 2006, "Spencer Janssen" <spencerjanssen at gmail.com> 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.




-------------- next part --------------
A non-text attachment was scrubbed...
Name: inits-print-5000.pdf
Type: application/pdf
Size: 2430 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/libraries/attachments/20060408/5c7e778a/inits-print-5000-0001.pdf
-------------- next part --------------
A non-text attachment was scrubbed...
Name: inits-orig-print-5000.pdf
Type: application/pdf
Size: 2734 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/libraries/attachments/20060408/5c7e778a/inits-orig-print-5000-0001.pdf
-------------- next part --------------
A non-text attachment was scrubbed...
Name: inits-orig-length-20000.pdf
Type: application/pdf
Size: 3339 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/libraries/attachments/20060408/5c7e778a/inits-orig-length-20000-0001.pdf
-------------- next part --------------
A non-text attachment was scrubbed...
Name: inits-length-500000.pdf
Type: application/pdf
Size: 1456 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/libraries/attachments/20060408/5c7e778a/inits-length-500000-0001.pdf


More information about the Libraries mailing list