[Haskell-beginners] stack overflow summing numbers read from a big file

Axel Wegen axel.wegen at gmail.com
Sat Mar 23 16:29:38 CET 2013


mukesh tiwari <mukeshtiwari.iiitm at gmail.com> writes:
> It's already mentioned there "A String is represented as a list of
> Char values; each element of a list is allocated individually, and has
> some book-keeping overhead. These factors affect the memory
> consumption and performance of a program that must read or write text
> or binary data. On simple benchmarks like this, even programs written
> in interpreted languages such as Python can outperform Haskell code
> that uses String by an order of magnitude".

I assumed that just means using plain Strings for that job will take
more time.

> import qualified Data.ByteString.Lazy.Char8 as BS
> import Data.Maybe ( fromJust )
>
> readI :: BS.ByteString -> Integer
> readI = fst . fromJust . BS.readInteger 
>
> main = BS.interact sumFile where
> sumFile = BS.pack . show . sum . map readI . BS.words

I get the same result, the stack overflow. Though I don't have wait as
long for it to break.

I think that there is something about the summation that makes it
impossible for the compiler to do it's magic and optimize the thing to
something less stack overflowing. I just don't understand what that is.

--
Axel Wegen



More information about the Beginners mailing list