Haskell Space Overflow

后端 未结 4 967
情深已故
情深已故 2020-12-10 05:51

I\'ve compiled this program and am trying to run it.

import Data.List
import Data.Ord
import qualified Data.MemoCombinators as Memo

collatzLength :: Int -&g         


        
4条回答
  •  天涯浪人
    2020-12-10 06:34

    Here's a shorter program that fails in the same way:

    main = print (maximum [0..1000000])
    

    Yep.

    $ ghc --make harmless.hs && ./harmless
    [1 of 1] Compiling Main             ( harmless.hs, harmless.o )
    Linking harmless ...
    Stack space overflow: current size 8388608 bytes.
    Use `+RTS -Ksize -RTS' to increase it.
    

    With -O2 it works. What do I make of it? I don't know :( These space mysteries are a serious gotcha.

    Edit:

    Thx to hammar for pointing out the culprit.

    Changing your program to use

    maximum' = foldl1' max
    

    Makes it work without -O2. The implementation of Prelude's maximum is lazy and so doesn't quite work for long lists without compiler magic dust.

提交回复
热议问题