[英]How Can I get floor with sqrt in type Integral
我在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)
Integral
是一個類型類,因此不應將其用作類型。 您甚至在isPrime
類型注釋中正確使用了它。 因此,要使其正常工作,您應該編寫
roundSqrt :: Integral t => t -> t
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.