【发布时间】:2025-12-05 05:15:02
【问题描述】:
我正在尝试创建一个基本函数来测试 Haskell 中整数的素数。我的代码可以在特定意义上工作,但是当我尝试将它传递给函数时继续收到错误消息。请注意,我使用:{ 和:} 直接在GHCi 中编写定义。
这个想法是创建一个以 N 为模{所有整数到四舍五入的 sqrt (N)} 的列表,然后测试结果列表中除初始索引之外的零。以下四个函数都起作用:
rndRoot :: (Floating a, Integral c, RealFrac a) => a -> c
rndRoot = round . sqrt
oneToRndRoot :: (Floating a, Integral t, RealFrac a) => a -> [t]
oneToRndRoot x = [1..rndRoot(x)]
modulo x y
| n < 0 = x
| otherwise = modulo n y
where n = x - y
mapMod x = map (modulo x)
这也有效:
mapMod 49 (oneToRndRoot 49)
然而,虽然 repl 毫无怨言地接受了这个定义......
mapModToRndRoot x = mapMod x $ oneToRndRoot x
...当我尝试使用它时它会吐出以下错误消息:
Prelude> mapModToRndRoot 39
<interactive>:475:1:
No instance for (Floating a0) arising from a use of ‘it’
The type variable ‘a0’ is ambiguous
Note: there are several potential instances:
instance Floating Double -- Defined in ‘GHC.Float’
instance Floating Float -- Defined in ‘GHC.Float’
In the first argument of ‘print’, namely ‘it’
In a stmt of an interactive GHCi command: print it
似乎工作正常的临时解决方案只是使用两个参数而不是重复相同的参数
mapModToRndRoot2 x y = map (modulo x) (oneToRndRoot y)
Prelude> mapModToRndRoot2 33 33
[0,1,0,1,3,3]
【问题讨论】:
-
您使用的是哪个版本的 GHC?我问是因为我得到的错误与你的略有不同。 GHCi 输入中的
lets 在问题的原始版本中(并且我已经编辑掉了......)可能表明您没有使用 GHC 8。 -
7.10.3,我会升级
-
虽然如果可以升级是个好主意,但请注意,您仍然会遇到同样的问题,正如 Fried Brice 所解释的那样,只是出现了不同的错误消息。 (我只询问了 GHC 版本,以确保在撰写答案时不会遗漏任何可能相关的细节。)
-
为了提高效率,您最好避免在整数和浮点数之间切换,方法是将 oneToRndRoot 中的条件 $n
标签: haskell discrete-mathematics number-theory