Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell divide Double by Int

Tags:

haskell

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?

like image 949
yonutix Avatar asked Sep 06 '25 03:09

yonutix


1 Answers

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)
like image 75
Chris Taylor Avatar answered Sep 08 '25 23:09

Chris Taylor