我在Haskell中编写了简单的函数,我想检查数字是否是素数较小的优化。我不知道如何写正确的roundSqrt
roundSqrt :: Integral -> Integral
roundSqrt x = floor (sqrt (fromIntegral x))
isPrime :: Integral t => t -> Bool
isPrime n = [i | i <- [2..k], n `mod` i == 0] == []
where k = roundSqrt(n)
答案 0 :(得分:0)
Integral
是类型类,因此不应将其用作类型。您甚至在isPrime
类型注释中正确使用了它。所以,要使它工作,你应该写
roundSqrt :: Integral t => t -> t