[Haskell-beginners] Stack overflow folding a map

tcollin371 tcollin371 at earthlink.net
Sat Feb 16 10:13:14 CET 2013

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)
      = Map.foldlWithKey' outputIfNotFilteredCount1 0 innerDB

main = do
      count   = Map.foldlWithKey' outputDatabaseCount1 0 resultDB
    putStrLn $ "Database entries:" ++ (show count)

Any thoughts on why this is running out of stack space?


More information about the Beginners mailing list