Created
October 4, 2010 07:00
-
-
Save kgbu/609331 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ghci> e: test.hs | |
| is_prime :: (Integral a, RealFrac a, Floating a) => a -> Bool | |
| is_prime x | x < 3 = True | |
| | otherwise = not (any (mod0_p x) (candidates x)) | |
| mod0_p :: (Integral a) => a -> a -> Bool | |
| mod0_p x y = (mod x y ) == 0 | |
| candidates :: (Integral t, Floating a, RealFrac a) => a -> [t] | |
| candidates x = [y| y <- [2 .. (floor (sqrt x))]] | |
| :load test.hs | |
| Main> is_prime 6 | |
| interactive>:1:0: | |
| Ambiguous type variable `t' in the constraints: | |
| `Floating t' | |
| arising from a use of `is_prime' at <interactive>:1:0-9 | |
| `Integral t' | |
| arising from a use of `is_prime' at <interactive>:1:0-9 | |
| `RealFrac t' | |
| arising from a use of `is_prime' at <interactive>:1:0-9 | |
| Probable fix: add a type signature that fixes these type variable(s) |
Author
Author
単純にfromIntegralという関数を使って型変換するしかないみたい。
is_prime :: (Integral a) => a -> Bool
is_prime x | x < 3 = True
| otherwise = not (any (mod0_p x) (candidates x))
mod0_p :: (Integral a) => a -> a -> Bool
mod0_p x y = (mod x y ) == 0
candidates :: (Integral t, Integral a) => a -> [t]
candidates x = [y| y <- [2 .. (floor (sqrt (fromIntegral x)))]]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
なんでなのかしら。
intの世界から少しはみ出すとえらい目に会うな。