4

以下返回 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
4

2 回答 2

10

让我们稍微混淆一下:

Prelude> (\x -> x `mod` (floor . sqrt) x) 2

<interactive>:1:24:
    Ambiguous type variable `b' in the constraints:
      `Floating b' arising from a use of `sqrt' at <interactive>:1:24-27
      `Integral b' arising from a use of `mod' at <interactive>:1:7-30
      `RealFrac b' arising from a use of `floor' at <interactive>:1:16-20
    Probable fix: add a type signature that fixes these type variable(s)

您将 的值n用作浮点数,将其传递给sqrtand floor。然后,您将该结果用作 int,将该结果传递给mod. 编译器无法为所有这些实例命名类型。

换句话说,它在您的第一个示例中起作用的原因

Prelude> 2 `mod` (floor . sqrt) 2
0

是因为您使用了两种不同的数字文字。一个可以是 int,一个可以是 float。如果您对两者使用相同的值,则需要调用fromIntegral将 int 转换为 float。

您可以通过添加类型签名来获得不同的错误消息,更改[2..][2..] :: [Integer]

No instance for (RealFrac Integer)
  arising from a use of `floor' at <interactive>:1:52-64
No instance for (Floating Integer)
  arising from a use of `sqrt' at <interactive>:1:58-63

这可能会让您更清楚地将值n用作两种不同的类型。

于 2011-09-08T21:08:40.477 回答
-1

正如下面 CA McCann 所指出的,我的回答不正确:-)

据我所知,这是因为您生成的列表可以包含任何实例,Floating因为类型签名sqrt

sqrt :: Floating a => a -> a

sqrt通过与预组合fromIntegral :: (Integral a, Num b) => a -> b,您可以获得所需的结果:

    Prelude> take 10 $ [n | n <- [2..], length [f | f <- [2..(floor(sqrt (fromIntegral n)))], n `mod` f == 0 ] == 0 ] 
[2,3,5,7,11,13,17,19,23,29]
于 2011-09-08T21:05:03.550 回答