【发布时间】:2014-04-29 12:49:23
【问题描述】:
我有以下功能:
probIndex idx xs =
xs!!idx+mult
where mult = round(2**idx)
当我尝试在 ghci 中加载它时,它显示以下错误:
Prelude> :load bn.hs
[1 of 1] Compiling Main ( bn.hs, interpreted )
bn.hs:31:16:
No instance for (RealFrac Int)
arising from a use of `round'
Possible fix: add an instance declaration for (RealFrac Int)
In the expression: round (2 ** idx)
In an equation for `mult': mult = round (2 ** idx)
In an equation for `probIndex':
probIndex idx xs
= xs !! idx + mult
where
mult = round (2 ** idx)
bn.hs:31:23:
No instance for (Floating Int)
arising from a use of `**'
Possible fix: add an instance declaration for (Floating Int)
In the first argument of `round', namely `(2 ** idx)'
In the expression: round (2 ** idx)
In an equation for `mult': mult = round (2 ** idx)
Failed, modules loaded: none.
Prelude>
为什么会发生? 2**idx 返回浮点数,但 round 将其转换为整数,所以一切都是整数。那些“Floating”和“RealFrac”是从哪里来的?
【问题讨论】:
标签: haskell