Float as Int format when printing in Haskell

This Haskell program prints "1.0". How can I get it to print "1"?

fact 0 = 1 fact x = x * fact (x-1) place mn = (fact m) / (fact n) * (fact (mn)) main = do print (place 0 0) 
+6
source share
2 answers

Using the / operation, you ask haskell to use a fractional data type. You probably don't want this. It is preferable to use an integral type such as Int or Integer . Therefore, I suggest doing the following: 1. Add a type declaration for the fact function, something like fact :: Integer -> Integer 2. Use quot instead of / .

So your code should look like this:

 fact :: Integer -> Integer fact 0 = 1 fact x = x * fact (x-1) place :: Integer -> Integer -> Integer place mn = (fact m) `quot` (fact n) * (fact (mn)) main = do print (place 0 0) 

Also, as @leftaroundabout pointed out , you probably want to use a better algorithm to calculate these binomial numbers.

+10
source

You can just use round :

print (round $ place 0 0)

This changes the format to the one you want. redneb's answer, however, is the right approach.

+2
source

All Articles