我有以下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^3taylor_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 15:41:30
哈斯克尔似乎是在推断taylor3s的返回是一个Integral类型,但是f等子函数被推断为处理Fractional类型的事实违反了这一推断。
也许通过显式地告诉Haskell taylor3s返回类型可能会有所帮助。
https://stackoverflow.com/questions/2839298
复制相似问题