我正在学习Haskell。我已经创建了函数,它返回乘法表到'b'的基数'n'。数字填充为'w'数字。作为最后一步,我想自动计算'w'。为什么不能编译?没有实例(浮动INT)
-- Number of digits needed for the multiplication table n*n in base 'base'
nOfDg :: Int -> Int-> Int
nOfDg n base = 1 + floor (logBase base (n*n))
错误:
No instance for (Floating Int)
arising from a use of `logBase' at C:\haskel\dgnum.hs:4:24-38
Possible fix: add an instance declaration for (Floating Int)
In the first argument of `floor', namely `(logBase b (n * n))'
In the second argument of `(+)', namely `floor (logBase b (n * n))'
In the expression: 1 + floor (logBase b (n * n))
我想你还需要'(fromIntegral base)'。 –
@Jason:不是Dan提供这种功能的类型,你不会。 – Chuck
是的,取决于你想如何使用它。要么就像我拥有它,如果可以更改类型签名,或者如果不是,则使用Andy的版本。 –