[Haskell-cafe] stack overflow pain
Heinrich Apfelmus
apfelmus at quantentunnel.de
Wed Sep 21 09:39:11 CEST 2011
Tim Docker wrote:
> I'm getting a stack overflow exception in code like this:
>
> -- applyAction :: A -> IO [B]
>
> ....
> vs <- fmap concat $ mapM applyAction sas
> return vs
>
> I don't get it if I change the code to this:
>
> -- applyAction :: A -> IO [B]
>
> ....
> mapM_ applyAction sas
> return []
>
> But of course, I need the results from the actions. I know that
> the returned list contains approximately 1 million items.
>
> Any suggestions on how I should rewrite the first code snippet
> to not blow the stack?
Of course, a list of 1 million items is going to take a lot of memory,
unless you generate it lazily. Unfortunately mapM cannot generate its
result lazily because it has to execute all IO actions before returning
the list of results.
I'm not entirely sure whether the stack overflow happens in this part of
your code, though. What happens if you change it to
map_ applyAction sas
return [1..1000000]
? If this still throws a stack overflow, then problem is in the part of
the code that consumes said list.
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
More information about the Haskell-Cafe
mailing list