[Haskell-beginners] [Int] oddness with summing large lists

Philip Scott haskell-beginners at foo.me.uk
Wed Apr 28 17:32:10 EDT 2010


Hi all,

I kind of stumbled into this while doing something else - but I thought 
it was worth posting since I have never actually managed to crash ghci 
before :)

Prelude> let d = [1..1000000000000] :: [Int]
Prelude> sum d
0

0?  I mean, I might have expected an integer overflow, but 0?

And now the really odd part... take away one zero

Prelude> let d = [1..100000000000] :: [Int]
Prelude> sum d
< ghci crashes and quits >

A slightly more reasonable number..

Prelude> let d = [1..10000000] :: [Int]
Prelude> sum d
*** Exception: stack overflow

At least I can appreciate what is going on in this one  :)

I'm aware that this is a silly way to sum from 1 to n, but I am at a 
loss to explain the above behavior (Except the stack overflow, that is 
fair enough).
I'm also aware that Int is a machine Int, not a clever infinite haskell Int.

Any ideas?

All the best,

Phil


More information about the Beginners mailing list