首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修正这个haskell对数函数?

如何修正这个haskell对数函数?
EN

Stack Overflow用户
提问于 2012-01-15 15:06:19
回答 1查看 539关注 0票数 2
代码语言:javascript
复制
natlog x = until cond count (1,1,0)
    where 
        cond (_,val,_) = val < 0.001
        count (i,val,sum) = (i+1,(-x)^i/i,sum+val)

此函数尝试计算log 1+x

这个问题与What is the type signature of this Haskell function?类似。

错误码:

代码语言:javascript
复制
<interactive>:1:8:
    Ambiguous type variable `t0' in the constraints:
      (Num t0) arising from the literal `1' at <interactive>:1:8
      (Integral t0) arising from a use of `natlog' at <interactive>:1:1-6
      (Fractional t0) arising from a use of `natlog'
                      at <interactive>:1:1-6
    Probable fix: add a type signature that fixes these type variable(s)
    In the first argument of `natlog', namely `1'
    In the expression: natlog 1
    In an equation for `it': it = natlog 1
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-15 15:18:15

问题是,由于^,您的输入需要是一个Integral,而由于/,您的输入需要是一个Fractional。通过对其中之一使用不同的运算符,您可以轻松地修复此问题;例如,使用**而不是^

代码语言:javascript
复制
natlog x = until cond count (1,1,0)
    where 
        cond (_,val,_) = val < 0.001
        count (i,val,sum) = (i+1,(-x)**i/i,sum+val)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8868111

复制
相关文章

相似问题

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