我有以下Haskell代码:问题Haskell的数类型
fac n = product [1..n]
taylor3s w0 f f' f'' t h = w1 : taylor3s w1 f f' f'' (t+h) h
where hp i = h^i/fac i
w1 = w0 + (hp 1) * f t w0 + (hp 2) * f' t w0 + (hp 3) * f'' t w0
taylor_results = take 4 $ taylor3s 1 f f' f'' 1 0.25
where f t x = t^4 - 4*x/t
f' t x = 4*t^3 - 4*(f t x)/t + 4*x/t^2
f'' t x = 12*t^2 - 4*(f' t x)/t + 8*(f t x)/t^2 - 8*x/t^3
taylor_results应该是taylor3s的使用情况。但是,数字类型推断有问题。当我尝试编译时,这是我得到的错误:
practice.hs:93:26:
Ambiguous type variable `a' in the constraints:
`Integral a'
arising from a use of `taylor3s' at practice.hs:93:26-51
`Fractional a' arising from a use of `f' at practice.hs:93:37
Possible cause: the monomorphism restriction applied to the following:
taylor_results :: [a] (bound at practice.hs:93:0)
Probable fix: give these definition(s) an explicit type signature
or use -XNoMonomorphismRestriction
有人能帮我理解问题是什么吗?
我总是为顶级函数提供显式类型。它使代码更易于阅读,当发生这样的事情时,它有助于缩小问题范围,并帮助编译器提供有意义的错误消息。如果没有这些,你知道在那里有一个类型错误。 – 2010-05-15 07:52:47
是的,我通常也会,但我对数字类型很无知。谁知道可能有这么多不同类型的数字? – mindeavor 2010-05-15 10:47:44