[Haskell-beginners] Stack overflow folding a map

tcollin371 tcollin371 at earthlink.net
Mon Feb 18 08:27:56 CET 2013

It turns out that the problem isn't the counting code in the fold operations, but instead seems to be lazy evaluation that's happening as the map is built.  If I run through the count operation every few thousand inserts, then I don't get the stack overflow operation.


-----Original Message-----
>From: tcollin371 <tcollin371 at earthlink.net>
>Sent: Feb 16, 2013 1:13 AM
>To: Hask <beginners at haskell.org>
>Subject: [Haskell-beginners] Stack overflow folding a map
>I've been playing around with Haskell on and off for a while, and am getting a "Stack space overflow" that I can't seem to correct.  I have a map containing about 2 million items.  Actually, it's a map keyed off of an Int where the element is another map keyed off of a string that has three Ints as its data.  The number of top level maps is pretty small (< 50), and the maps held by them hold a lot of entries.
>I want to walk through all of the entries and gather some statistics, but I keep getting the stack overflow.  I worked down my test to an inner and outer fold that just counts the entries, and I still get the stack overflow.  I added 'seq' calls to make sure that the count isn't building up a bunch of undone operations, and I'm using foldlWithKey, and have also tried foldlWithKey' with the same result.
>I get the count in my main function and immediately print it out.  Here is the relevant code:
>outputIfNotFilteredCount1 :: Int -> KeyMap -> PuzzleMapRecord -> Int
>outputIfNotFilteredCount1 inCount key entry
>  = seq inCount (inCount + 1)
>outputDatabaseCount1 :: Int -> Int -> InnerDB -> Int
>outputDatabaseCount1 inCount smndCount innerDB 
>  = seq inCount (inCount + outCount)
>  where
>    outCount
>      = Map.foldlWithKey' outputIfNotFilteredCount1 0 innerDB
>main = do
>    let
>      count   = Map.foldlWithKey' outputDatabaseCount1 0 resultDB
>    putStrLn $ "Database entries:" ++ (show count)
>Any thoughts on why this is running out of stack space?
>Beginners mailing list
>Beginners at haskell.org

More information about the Beginners mailing list