gpt4 book ai didi

haskell - 为什么这个 Haskell 在扩展时会提示类型不明确?

转载 作者:行者123 更新时间:2023-12-02 10:17:51 24 4
gpt4 key购买 nike

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

最佳答案

让我们稍微澄清一下:

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 的值用作 float ,并将其传递给 sqrtfloor。然后,您将该结果用作 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 的值用作两种不同的类型。

关于haskell - 为什么这个 Haskell 在扩展时会提示类型不明确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7354317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com