我最近参加了竞争性的编码竞赛.
这个Haskell在运行ghc 7.6.3的判断系统上发生了空间泄漏:
t n [] = 0
t n ('8':rest) = t (n+1) rest
t n (')':rest) = n + (t n rest)
main = getLine >>= (\l -> print (t 0 l))
Run Code Online (Sandbox Code Playgroud)
比赛结束后,测试用例发布.其中一个失败案例是:(一个包含10 ^ 5个关闭的parens的文件):https://cses.fi/download/1/b575d19a75bf724b50fa4a399f8187b6d6edb4ccb62bd1a774f9294969152e46
错误是
Stack space overflow: current size 8388608 bytes. Use `+RTS -Ksize -RTS' to increase it.
Run Code Online (Sandbox Code Playgroud)
我也知道代码是用-O2和-Wall编译的,我认为是GHC 7.6.3.
对于我的生活,我无法在我的机器上使用GHC 8.0.2或7.10.3重现错误.
代码中是否有明显的空间泄漏?
编辑:
我测试了下面建议的代码和这样实现的折叠:
import Data.Foldable
t (kasit, score) '8' = (kasit+1, score)
t (kasit, score) _ = (kasit, score+kasit)
main = …Run Code Online (Sandbox Code Playgroud)