Haskell Time Limit on Evaluation

haskell, time

Solution

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

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 ->`.

Problem

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) ``` 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.

Original source