此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)
通过使用该/
操作,您要求haskell使用分数数据类型。在这种情况下,您可能不希望这样做。最好使用诸如Int
或的整数类型Integer
。因此,我建议您执行以下操作:1.为fact
函数添加类型声明,类似于fact :: Integer -> 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指出的那样,您可能希望使用更好的算法来计算这些二项式数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句