我遇到了Haskell函数的输入问题。 我实现了这个(简化的)函数:
function (xa,ya,za) (xb,yb,zb) (Size tai) = function (xa,ya,za) (xb,yb,zb) (Ndiv ndiv)
where
ndiv = ceiling (leng / tai)
leng = sqrt((xb-xa)**2+(yb-ya)**2+(zb-za)**2)
data Method = Ndiv Int
| Size Double
如果我提供function
此签名,则效果很好:
function :: (Double,Double,Double) -> (Double,Double,Double) -> Method -> [(Double,Double,Double)]
现在,我想将我的函数扩展到整个Num
类。我强迫这个类型:
function :: Num a => (a,a,a) -> (a,a,a) -> Method -> [(a,a,a)]
编译时,GHC给出了以下错误:
Could not deduce (a ~ Double)
from the context (Num a)
bound by the type signature for
function :: Num a =>
(a, a, a) -> (a, a, a) -> Method -> [(a, a, a)]
at Type.hs:7:13-62
`a' is a rigid type variable bound by
the type signature for
function :: Num a =>
(a, a, a) -> (a, a, a) -> Method -> [(a, a, a)]
at Type.hs:7:13
In the second argument of `(/)', namely `tai'
In the first argument of `ceiling', namely `(leng / tai)'
In the expression: ceiling (leng / tai)
我从来没有这样的错误,我有点失望 我怀疑班级冲突/不匹配,但我不知道如何解决它?
您知道我的功能有什么问题以及如何使其正常工作吗?
答案 0 :(得分:3)
由于您的数据类型,tai
参数固定为Double
。将其更改为
data Method a = Ndiv Int | Size a
例如,那么你的函数应该进行类型检查,尽管你需要比Num
更强的约束,因为cieling
需要RealFrac
。类型将是
function :: RealFrac a => (a,a,a) -> (a,a,a) -> Method a -> [(a,a,a)]