在GHCiOnline中,let listRestDiv 4 = floor(4/(fromInteger (nextPrimeDiv 4)))
确实有效,但一般
let listRestDiv n = floor(n/(fromInteger (nextPrimeDiv n)))
不接受,其中nextPrimeDiv n
由
let nextPrimeDiv n = head [x|x<-[1..n], n `mod` x == 0, x/=1]
然后返回下一个主要除数。
它说:
无法演绎(RealFrac Integer)因使用`floor&#39;从 上下文(积分b)由推断类型的listRestDiv ::绑定 积分b =&gt;整数 - &gt; b在
并在那里结束。我只是不知道这里的类型。 提前感谢任何暗示。
答案 0 :(得分:4)
收到的实际完整错误消息,提示我们解决此问题。
Prelude> let listRestDiv n = floor(n/(fromInteger (nextPrimeDiv n)))
<interactive>:8:21:
Could not deduce (RealFrac Integer) arising from a use of `floor'
from the context (Integral b)
bound by the inferred type of
listRestDiv :: Integral b => Integer -> b
at <interactive>:8:5-59
Possible fix: add an instance declaration for (RealFrac Integer)
In the expression: floor (n / (fromInteger (nextPrimeDiv n)))
In an equation for `listRestDiv':
listRestDiv n = floor (n / (fromInteger (nextPrimeDiv n)))
<interactive>:8:28:
Could not deduce (Fractional Integer) arising from a use of `/'
from the context (Integral b)
bound by the inferred type of
listRestDiv :: Integral b => Integer -> b
at <interactive>:8:5-59
Possible fix: add an instance declaration for (Fractional Integer)
In the first argument of `floor', namely
`(n / (fromInteger (nextPrimeDiv n)))'
In the expression: floor (n / (fromInteger (nextPrimeDiv n)))
In an equation for `listRestDiv':
listRestDiv n = floor (n / (fromInteger (nextPrimeDiv n)))
这里需要注意的重要事项是
Possible fix: add an instance declaration for (Fractional Integer)
In the first argument of `floor', namely
`(n / (fromInteger (nextPrimeDiv n)))'
所以,你所要做的就是,fromInteger
也使用n
,就像这样
Prelude> let listRestDiv n = floor(fromInteger n/(fromInteger (nextPrimeDiv n)))