
Hi Alex
I am no expert but try to profile your code and see the memory usage. It
seems like the sum function is causing the stack overflow[1].
Try this one.
import qualified Data.ByteString.Lazy.Char8 as BS
import Data.Maybe ( fromJust )
import Data.List
readI :: BS.ByteString -> Integer
readI = fst . fromJust . BS.readInteger
main = BS.interact $ sumFile where
sumFile = BS.pack . show . sum' . map readI . BS.words
sum' = foldl' (+) 0
-Mukesh
[1] http://www.haskell.org/haskellwiki/Memory_leak
On Sat, Mar 23, 2013 at 8:59 PM, Axel Wegen
mukesh tiwari
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
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners