我正在尝试在Haskell中编写一个安全的超时评估函数.代码如下
import System.Timeout
compute, compute' :: Int -> Int
compute i = sum [1..300000 + i]
compute' i = last $ repeat i
timedComp :: Int -> a -> IO (Maybe a)
timedComp timeLeft toCompute =
timeout timeLeft go
where
go = toCompute `seq` return toCompute
main = do
res <- timedComp 10000 (compute 0)
print res
res' <- timedComp 10000 (compute' 0)
print res'
Run Code Online (Sandbox Code Playgroud)
(我知道我只评价WHNF.)
当我运行main时,我只输出一个Nothing,然后程序挂起.我试图编译并运行程序多线程,但它没有帮助.试用GHC 7.6.3和7.8.3.有什么建议?