(Floating Int)没有实例 [英] No instance for (Floating Int)

查看:76
本文介绍了(Floating Int)没有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Haskell。我已经创建了函数,它返回乘法表到'b'的基数'n'。数字填充为'w'数字。作为最后一步,我想自动计算'w'。为什么不能编译?

   - 乘法表所需的位数n * n在基数'base'中
nOfDg :: Int - >内部 - > Int
nOfDg n base = 1 + floor(logBase base(n * n))

错误:

 没有使用C:\中的`logBase'引发的(Floating Int)
实例haskel \dgnum.hs:4:24-38
可能的修正:为(Floating Int)添加实例声明
在'floor'的第一个参数中,即'(logBase b(n * n )'
在'(+)'的第二个参数中,即'floor(logBase b(n * n))'
在表达式:1 + floor(logBase b(n * n) )


解决方案

logBase接受两个实现浮动类型类型的参数。在将参数传递给logBase之前,您需要从参数中调用参数。这为6.10.3编译:

  nOfDg :: Int  - >内部 - > Int b $ b nOfDg n base = 1 + floor(logBase(fromIntegral base)(fromIntegral(n * n)))

你必须记住Haskell是非常强类型的,所以你不能仅仅假设提供给你的函数的Int参数会被自动强制为日志函数通常采用的浮动数字。


I am learning Haskell. I have created function which returns multiplication table up to 'n' in base 'b'. Numbers are padded to 'w' digits. As the last step, I want to compute 'w' automatically. Why does this not compile?

-- 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)) 

error:

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))

解决方案

logBase takes two parameters which implement the floating typeclass. You'll need to call fromIntegral on the parameters before passing them to logBase. This compiled for me with 6.10.3:

nOfDg :: Int -> Int-> Int
nOfDg n base = 1 + floor ( logBase (fromIntegral base) (fromIntegral (n*n)))

You have to remember that Haskell is very strongly typed, so you can't just assume that the Int parameters supplied to your function will automatically be coerced to the floating numbers that log functions generally take.

这篇关于(Floating Int)没有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆