I have the following piece of code, the problem is that I try to divide a Double
by an Int
factorial :: Int -> Int
factorial 0 = 1
factorial e = e * (factorial e-1)
sumX :: Double -> Int -> Double
sumX x 0 = (x^0) / (factorial 0)
How can I get it to work?
One problem is that you have incorrectly parenthesized your factorial function. You should write
factorial e = e * factorial (e - 1)
Secondly, you can use the fromIntegral
function to convert any integral type (an instance of the Integral
class) to any numeric type (an instance of the Num
class)
sumX x 0 = x ^ 0 / fromIntegral (factorial 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With