2016-07-24 94 views
1

我开始学习Haskell,并且必须创建一个非常简单的函数,它需要两个列表并将它们连接起来。通过两个列表时,Haskell类型匹配错误

app :: [a] -> [a] -> [a] 
app xs ys = xs ++ ys 

这是一个任务的一部分,我们必须对这些小函数进行基准测试。
我用Criterion来做这件事。完整的代码如下:

import Criterion.Main 
main = defaultMain [ 
    bgroup "normal 100" [ bench "app"  $ whnf app $ [0..49] [50..100] 
         ] 
        ] 
app :: [a] -> [a] -> [a] 
app xs ys = xs ++ ys 

的编译失败,给我留下了这一点:

Couldn't match expected type `[Integer] -> [a0]' 
      with actual type `[Integer]' 
The function `[0 .. 49]' is applied to one argument, 
but its type `[Integer]' has none 
In the second argument of `($)', namely `[0 .. 49] [50 .. 100]' 
In the second argument of `($)', namely 
    `whnf app $ [0 .. 49] [50 .. 100]' 

我在解密GHC错误消息的实际问题,在这里我基本上停留。
我知道这里有很多关于类型不匹配的问题,但我找不到解决方案。
在此先感谢!

+0

我会推荐在不使用'$'的情况下重写这个文件,只使用圆括号,然后在你真正想要的时候加入'$'。 –

+0

@AlexisKing是的,我已经试过,但我得到了同样的错误,所以我把他们留在。 – DShade

回答

3

benchwhnf的签名是:

bench :: String  -> Benchmarkable -> Benchmark 
whnf :: (a -> b) -> a -> Benchmarkable 

由于app有两个参数,我们需要库里调用第一个whnf

whnf (app [0..49]) [50..100] :: Benchmarkable 

注意whnf有两个参数:(app [0..49])[50..100]

现在,我们可以形成呼叫bench

bench "app" (whnf (app [0..49]) [50..100]) :: Benchmark 

如果我们要使用$,只有一个地方,我们可以使用它:

bench "app" $ whnf (app [0..49]) [50..100] 

我们不能把后因为一般whnf一个$:

a b c == (a b) c 

a $ b c == a (b c) 
+0

这是有道理的。非常感谢! – DShade