我是Haskell的绝对初学者,我正在尝试构建一个生成素数的列表理解。我尝试调用我的函数时收到错误。我真的不确定导致错误的原因。任何人都可以解释一个绝对的初学者会理解的问题是什么?我只是在Haskell类型系统的基础知识。
代码:
roundupsqrt x = ceiling (sqrt x)
listnthprimes x = take x [y|y<-[1..], odd y, all (/=0) (map (y`mod`) [2..(roundupsqrt y)])]
错误:
No instance for (RealFrac a0) arising from a use of `listnthprimes'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance RealFrac Double -- Defined in `GHC.Float'
instance RealFrac Float -- Defined in `GHC.Float'
instance Integral a => RealFrac (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
In the expression: listnthprimes 6
In an equation for `it': it = listnthprimes 6
答案 0 :(得分:3)
sqrt
定义为Floating a => a -> a
;你传递一个整数。 fromIntegral
可行:
roundupsqrt x = ceiling (sqrt (fromIntegral x))