我正在學習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的版本。 –