在Haskell中使用括号

时间:2015-05-06 02:35:26

标签: haskell

我收到此错误:

<interactive>:145:29:
    Could not deduce (Integral ([a0] -> Int))
      arising from a use of ‘fromIntegral’
    from the context (Num ([a] -> a), Fractional a)
      bound by the inferred type of
               meanList :: (Num ([a] -> a), Fractional a) => [a] -> a
      at <interactive>:145:5-50
    The type variable ‘a0’ is ambiguous
    In the second argument of ‘(/)’, namely ‘(fromIntegral length x)’
    In the expression: (sum x) / (fromIntegral length x)
    In an equation for ‘meanList’:
        meanList x = (sum x) / (fromIntegral length x)

以上错误由以下方式生成:

meanList x = (sum x) / (fromIntegral length x)

但是,将此更新为:

let meanList x = sum x / fromIntegral (length x)

然后一切都很好。

括号如何在Haskell中工作?

1 个答案:

答案 0 :(得分:13)

函数应用程序是左关联的。换句话说,

fromIntegral length x = (fromIntegral length) x

因此错误Could not deduce (Integral ([a0] -> Int))因为长度类型确实没有Integral的实例。