此代码编译并运行没有问题:
module Main where
import Criterion.Main
main :: IO ()
main =
defaultMain
[env (return $ [1,2])
(\is ->
bgroup "group" (benchmarks is))]
timesTwo :: Int -> Int
timesTwo i = 2 * i
benchmarks :: [Int] -> [Benchmark]
benchmarks is = [ bench "foo" $ nf timesTwo (is !! 0)
, bench "foo" $ nf timesTwo (is !! 1) ]
但是,如果我将benchmarks
功能更改为
benchmarks :: [Int] -> [Benchmark]
benchmarks is = map (\i -> bench "foo" $ nf timesTwo i) is
它仍然可以编译,但我得到这个运行时错误:
ghci> main
*** Exception: Criterion atttempted to retrieve a non-existent environment!
Perhaps you forgot to use lazy pattern matching in a function which
constructs benchmarks from an environment?
(see the documentation for `env` for details)
我该如何解决这个问题?
如您所见,我的目标是映射从环境中获取的列表,以便将其转换为Benchmark
可以与 Criterion 一起使用的s列表。
注意:我最终想要使用比两个更多的元素,所以元组不是我想要的。