我使用的是lagrest素除数,但我对下面的代码有问题:
lpd :: Integer -> Integer
lpd n = helper n (2:[3,5..ceiling])
where
helper n divisors@(d:ds)
| n == d = n
| n `rem` d == 0 = helper (n `div` d) divisors
| otherwise = helper n ds
ceiling = truncate $ sqrt n错误消息为:
problems.hs:52:15:
No instance for (RealFrac Integer)
arising from a use of `truncate'
Possible fix: add an instance declaration for (RealFrac Integer)
In the expression: truncate
In the expression: truncate $ sqrt n
In an equation for `ceiling': ceiling = truncate $ sqrt n
problems.hs:52:26:
No instance for (Floating Integer)
arising from a use of `sqrt'
Possible fix: add an instance declaration for (Floating Integer)
In the second argument of `($)', namely `sqrt n'
In the expression: truncate $ sqrt n
In an equation for `ceiling': ceiling = truncate $ sqrt n
Failed, modules loaded: none.我的打字好像不太好。我该怎么做才能让这段代码工作呢?
发布于 2011-10-12 19:51:14
用sqrt $ fromIntegral n替换sqrt n。
问题是sqrt的类型是(Floating a) => a -> a,所以它不能处理整数。函数
fromIntegral :: (Integral a, Num b) => a -> b从整数类型到更通用的Num实例的“强制转换”。
https://stackoverflow.com/questions/7739598
复制相似问题