Alex Gordon
Alex Gordon

Reputation: 60751

Use of parentheses in Haskell

I am getting this error:

<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)

Above error is generated by:

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

However, when updating this to:

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

Then all is well.

How do parentheses work in Haskell?

Upvotes: 1

Views: 300

Answers (1)

Sarah
Sarah

Reputation: 6695

Function application is left associative. In other words,

fromIntegral length x = (fromIntegral length) x

Hence the error Could not deduce (Integral ([a0] -> Int)) because the type of length does indeed not have an instance of Integral.

Upvotes: 13

Related Questions