下面的程序应该检查输入 n 是否可以被 n 中的数字之和整除
module Harshad where
isHarshad :: Int -> Bool
isHarshad n = (mod n (sumDig n)) == 0)
where
sumDig n
| (floor (n / 10) == 0) = n -- n consists of only one digit
| otherwise = sumDig (floor (n / 10)) + floor (10*(n - floor (n / 10)))
我得到以下编译错误: * 没有因使用 `sumDig' 而产生的 (RealFrac Int) 实例
即使在尝试添加各种转换后,我仍然卡住了。
最佳答案
完全去掉小数,只使用 div
对于整数除法:
sumDig n = if n < 10 then n -- n consists of only one digit
else sumDig (n `div` 10) + (n `mod` 10)
关于haskell - 没有因使用 ' ... ' 而产生的 (RealFrac Int) 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42765318/