以下返回 True(因为 2147483647 是素数)。
length [f | f <- [2..(floor(sqrt 2147483647))], 2147483647 `mod` f == 0 ] == 0
当我尝试如下扩展它时,为什么它不起作用?
Prelude> [n | n <- [2..], length [f | f <- [2..(floor(sqrt n))], n `mod` f == 0 ] == 0 ]
<interactive>:1:39:
Ambiguous type variable `t' in the constraints:
`RealFrac t' arising from a use of `floor' at <interactive>:1:39-51
`Integral t' arising from a use of `mod' at <interactive>:1:56-64
`Floating t' arising from a use of `sqrt' at <interactive>:1:45-50
Probable fix: add a type signature that fixes these type variable(s)
不过我不明白,为什么使用地板会产生 RealFrac?我以为 floor 采用了 RealFracs 并生产了 Integrals?另外它并没有抱怨上面的例子,我只是像那时一样输入更多的整数。
Prelude> :t floor
floor :: (RealFrac a, Integral b) => a -> b