How do you get 100% code coverage with guards in Haskell?

Posted by dan_waterworth on Stack Overflow See other posts from Stack Overflow or by dan_waterworth
Published on 2011-01-11T15:04:59Z Indexed on 2011/01/11 15:53 UTC
Read the original article Hit count: 140

Filed under:
|

I'm trying to get (and prove) 100% test coverage for some code I'm writing in Haskell using HPC. However if I write something like this:

fac n | n > 0 = n * (fac (n - 1))
      | otherwise = 1

Then the second expression of the guard statement has always True tagged to it. What is the easiest way to overcome this in the general case?

edit: Just to clarify. This code:

fac n = if n > 0 then n * (fac (n - 1))
        else 1

Works fine with HPC, (running it gives 100% code coverage).

I'm basically suffering from this problem: http://hackage.haskell.org/trac/ghc/ticket/3175

© Stack Overflow or respective owner

Related posts about haskell

Related posts about code-coverage