[Haskell-cafe] Unresolved overloading error

Jacques Carette carette at mcmaster.ca
Sat Mar 31 10:42:30 EDT 2007


Bryan Burgers wrote:
> On 3/31/07, Scott Brown <doolagarl2002 at yahoo.com.au> wrote:
>>
>> It's working now, thank you.
>> I changed the definition to
>>
>> > binom n j = div (fac n) ((fac j)*(fac (n - j)))
>>
>> > bernoulli n p j = fromIntegral(binom n j)*(p ^ j) * ((1 - p)^(n - j))
>
> As a matter of style suggestion, it might make 'binom' more clear if
> you use 'div' as an infix operator:
>
>> binom n j = (fac n) `div` ( fac j * fac (n - j) )
And as a matter of efficiency, no one would write binom using factorial, 
but would rather write at least
binom u k = (product [(u-i+1)  | i <- [1..k]]) `div` (product [1..k])
and even better would be to do it this way
-- bb u k = toInteger $ product [ (u-i+1) / i | i <- [1..k]]
but that does not type (for a good reason).  The issue is that it is 
possible to prove that the above is an integer, but the compiler can't 
see that :-(
That can be done as

import Data.Ratio
bb u k = numerator $ product [ (u-i+1) / i | i <- [1..k]]

Of course, the above is fast if and only if the gcd operation in 
Data.Ratio has been well optimized.

Jacques


More information about the Haskell-Cafe mailing list