List 传递两个列表时出现Haskell类型匹配错误
我正在开始学习Haskell,并且必须创建一个非常简单的函数,该函数包含两个列表并将它们合并List 传递两个列表时出现Haskell类型匹配错误,list,haskell,type-mismatch,List,Haskell,Type Mismatch,我正在开始学习Haskell,并且必须创建一个非常简单的函数,该函数包含两个列表并将它们合并 app :: [a] -> [a] -> [a] app xs ys = xs ++ ys 这是我们必须对这些较小功能进行基准测试的任务的一部分。 我这样做是有标准的。完整代码如下所示: import Criterion.Main main = defaultMain [ bgroup "normal 100" [ bench "app" $ whnf app $ [0
app :: [a] -> [a] -> [a]
app xs ys = xs ++ ys
这是我们必须对这些较小功能进行基准测试的任务的一部分。我这样做是有标准的。完整代码如下所示:
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错误消息时遇到了一个真正的问题,我基本上被困在这里了我知道这里有很多关于类型不匹配的问题,但我找不到解决方案
提前谢谢 工作台和
whnf
的签名为:
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
及
我建议您在任何地方都不要使用
$
来重写它,只使用括号,然后如果您真的想让它工作的话,再添加$
。@AlexisKing是的,我已经尝试过了,但是我遇到了同样的错误,所以我把它们留在了里面。这是有道理的。谢谢!
a $ b c == a (b c)