简体   繁体   中英

Haskell Type matching error when passing two Lists

I'm getting started learning Haskell and have to create a really simple function that takes two Lists and concats them.

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

This is part of a task where we have to benchmark smaller functions like these.
I do this with Criterion. The complete code is as following:

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  

The compiling fails and leaves me with this:

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]'

I have a real problem in decrypting ghc error messages and am basically stuck here.
I know there are a lot of questions regarding type-mismatches here but i couldn't find a solution.
Thanks in advance!

The signatures of bench and whnf are:

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

Since app takes two arguments, we need to curry the first one in the call to whnf :

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

Note that whnf has two arguments: (app [0..49]) and [50..100] .

Now we can form the call to bench :

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

If we want to use $, there is only one place where we can use it:

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

We can't place a $ after whnf because in general:

a b c   ==  (a b) c

and

a $ b c == a (b c)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM