1

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)
          ]
MatejKr
  • 115
  • 12

1 Answers1

2

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.

fgv
  • 835
  • 8
  • 15
  • I yust did it like: bench "1" $ nf (binSear 5452700 0 9999999) arr and it works. Than you – MatejKr Jul 28 '15 at 14:11
  • @MatejKr I don't think the reordering was actually necessary in this case. What you need to ensure is that the function cannot do any real work before being applied to the final parameter, because otherwise that work could be cached and Criterion would only really benchmark the *first* iteration. (That's also the reason why you have to provide a function and argument separately in the first place.) – Ørjan Johansen Jul 28 '15 at 16:17