Haskell评估期限 [英] Haskell Time Limit on Evaluation
问题描述
没有人知道一个仅允许一定时间执行功能的功能.具有这种类型签名的东西.
Does anyone know of a function that will allow only a certain amount of time to execute a function. Something with a type signature like this.
limited::Int->(a->b)->a->IO (Maybe b)
我想不出如何实现,也找不到.我问的原因是,我将列出所有可能的 Brainfuck 程序的列表,以及我想过滤掉花费太长时间的内容.
I can't think of how to implement, and I couldn't find it. The reason why I ask is I am going to make a list of all possible Brainfuck programs, and I want to filter out the ones that take too long.
推荐答案
System中有专用功能超时:
There's a dedicated function from System.Timeout:
timeout :: Int -> IO a -> IO (Maybe a)
使用您的书写方式,只需使用
To have it the way you wrote, just use
limited t f x = timeout t $ do
let y = f x
y `seq` return y
请记住,Haskell的懒惰意味着任何值都被其他语言称为零参数的记忆函数",因此您实际上并不需要(a->b) -> a ->
.
Remember that Haskell's laziness means any value is what other languages might call "memoised function of zero arguments", so you don't really need the (a->b) -> a ->
.
这篇关于Haskell评估期限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!