I’m playing with lagrest prime divisor and I have troubles with this code:
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
The error message is:
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.
It seems that my typing is lacking. What could I do to make this code work?
Replace
sqrt nwithsqrt $ fromIntegral n.The problem is that
sqrthas type(Floating a) => a -> a, so it doesn’t work on integers. The function“casts” from integral types to more general
Numinstances.