[Haskell-cafe] curious about sum

Keith Sheppard keithshep at gmail.com
Mon Jun 15 20:49:58 EDT 2009


The answer is sometimes (only if you use an optimize flag):

keith at sugarglider:~/temp/> cat sumtest.hs
main = putStrLn . show . sum $ [0 .. 1000000]
keith at sugarglider:~/temp/> ghc --make sumtest.hs
[1 of 1] Compiling Main             ( sumtest.hs, sumtest.o )
Linking sumtest ...
keith at sugarglider:~/temp/> ./sumtest
Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize' to increase it.
keith at sugarglider:~/temp/> rm sumtest.hi sumtest.o sumtest
keith at sugarglider:~/temp/> ghc --make -O2 sumtest.hs
[1 of 1] Compiling Main             ( sumtest.hs, sumtest.o )
Linking sumtest ...
keith at sugarglider:~/temp/> ./sumtest
500000500000
keith at sugarglider:~/temp/> ghc --version
The Glorious Glasgow Haskell Compilation System, version 6.10.1

But since hackage warns against using these flags when you upload
packages I would think that most libraries would not be using a strict
version of sum.

On Mon, Jun 15, 2009 at 11:14 AM, Don Stewart<dons at galois.com> wrote:
> keithshep:
>> Is there any reason that sum isn't strict? I can't think of any case
>> where that is a good thing.
>>
>> Prelude> sum [0 .. 1000000]
>> *** Exception: stack overflow
>>
>
> It is strict when subject to strictness analysis (try compiling it).
>
> -- Don
>
>
>



-- 
keithsheppard.name


More information about the Haskell-Cafe mailing list