haskellbenchmarkingbinary-searchhaskell-criterion

Haskell Criterion - 'nf' is applied to too few arguments


I am a new guy to Haskell. I am working a benchmark(Criteriaon) on binary search algorithm. I keep getting error: 'nf' is applied to too few arguments what am I doing wrong.

Thanks

binSear array serNum lowInx highInx
   | highInx < lowInx       = -1
   | array!!sred > serNum = binSear array serNum lowInx (mid-1)
   | array!!sred < serNum = binSear array serNum (mid+1) highInx
   | otherwise            = mid
   where
   mid = lowInx + ((highInx - lowInx) `div` 2)

main = do
        let arr = [1..10000000]
        defaultMain [
            bench "1" $ nf (binSear arr 54527 0 9999999)
          ]

Solution

  • The type of nf is (a->b)->a->b, so it expects two parameters: a function and an input to that function. The function should produce a Benchmarkable.

    In your case, you are just passing one parameter to nf: the function itself, but that function is fully applied, so it's not expecting any additional parameter, nor are you passing that extra parameter. In this case, you should partially apply the function and pass that extra parameter to nf.

    You may be forced to reorder the parameters of binSear or create a helper lambda to do so, to ensure that the currying happens in the last parameter, and you should pass that parameter to nf outside of the parenthesis.