首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haskell数字类型的问题

Haskell数字类型的问题
EN

Stack Overflow用户
提问于 2010-05-15 15:31:29
回答 1查看 491关注 0票数 5

我有以下haskell代码:

代码语言:javascript
复制
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的一个用例。但是,数字类型推断有问题。当我尝试编译时,这是我得到的错误:

代码语言:javascript
复制
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

有没有人能帮我理解一下问题是什么?

EN

回答 1

Stack Overflow用户

发布于 2010-05-15 15:41:30

哈斯克尔似乎是在推断taylor3s的返回是一个Integral类型,但是f等子函数被推断为处理Fractional类型的事实违反了这一推断。

也许通过显式地告诉Haskell taylor3s返回类型可能会有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2839298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档