2012-01-15 9 views
2
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) 

Эта функция попытка сосчитать войти 1 + хКак исправить эту функцию логарифма haskell?

Проблема аналогична What is the type signature of this Haskell function?.

Код ошибки:

<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 

ответ

4

Вопрос заключается в том, что ваш вклад должен быть Integral из ^ и Fractional из /. Вы можете легко исправить это, используя другой оператор для одного из них; например, использовать ** вместо ^:

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) 
Смежные вопросы