在Haskell中打印时将Float格式化为Int

前端之家收集整理的这篇文章主要介绍了在Haskell中打印时将Float格式化为Int前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这个 Haskell程序打印“1.0”如何让它打印“1”?
fact 0 = 1
fact x = x * fact (x-1)

place m n = (fact m) / (fact n) * (fact (m-n))

main = do   
   print (place 0 0)

解决方法

通过使用/ operation,您要求haskell使用小数数据类型.在这种情况下,您可能不希望这样.最好使用整数类型,如Int或Integer.所以我建议你做以下事情:
1.为事实函数添加一个类型声明,类似于fact :: Integer – >整数
2.使用quot代替/.

所以你的代码应该是这样的:

fact :: Integer -> Integer
fact 0 = 1
fact x = x * fact (x-1)

place :: Integer -> Integer -> Integer
place m n = (fact m) `quot` (fact n) * (fact (m-n))

main = do   
   print (place 0 0)

此外,正如@leftaroundabout所指出的,您可能希望使用更好的算法来计算这些二项式数.

原文链接:https://www.f2er.com/css/214860.html

猜你在找的CSS相关文章