没有 (Floating Int) 实例
我正在学习哈斯克尔。我创建了一个函数,该函数返回基数“b”中最多“n”的乘法表。数字被填充为“w”位。作为最后一步,我想自动计算“w”。为什么这不能编译?
-- 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))
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))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
logBase 采用两个实现浮动类型类的参数。在将参数传递给 logBase 之前,您需要对参数调用 fromIntegral。这是用 6.10.3 为我编译的:
你必须记住 Haskell 是非常强类型的,所以你不能仅仅假设提供给你的函数的 Int 参数会自动强制转换为 log 函数通常采用的浮点数。
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:
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.
logBase
被声明为适用于浮点类型。 Int 不是浮点类型,Haskell 中没有自动转换。试试这个:logBase
is declared to work on floating point types. Int is not a floating point type and there is no automatic conversion in Haskell. Try this:从序言中:
这意味着使用 logBase 您必须使用浮动类型。但 Int 不是浮点类型,并且没有数字类型的自动转换,因此必须将其从 Int 转换为 Floating 类型:
toEnum 函数以 int 作为参数并返回“Enum”类型。好的部分是 Float 是 Enum 的一个实例,所以你可以使用它
你应该阅读/记录 Haskell 中数字类型的标准类型类(Num、Fractional、Integral、Floating...),因为它们经常弹出在代码中,学习转换可能很有用。
编辑:这本Haskell Wiki Book提供了标准类型类之间关系的非常有用的图形,包括数字类型。
From the Prelude :
It means that using logBase you must be using a floating type. But Int is not a floating Type, and there is no automatic conversion for numeric types, so you have to convert it from Int to a Floating type :
the toEnum function take an int as parameter and return an "Enum" type. The good part is that Float is an instance of Enum, so you could use it
You should read/document you about standard type classes in haskell for numeric types (Num,Fractional,Integral, Floating...) as they often pop-up in code, learning conversions could be useful.
Edit : this Haskell Wiki Book provide a very useful graphic of relationship between standards type class, including numeric types.