Search code examples
haskellsyntaxfunction-composition

Haskell dot operator


I try to develop a simple average function in Haskell. This seems to work:

lst = [1, 3]

x = fromIntegral (sum lst)
y = fromIntegral(length lst)

z = x / y

But why doesn't the following version work?

lst = [1, 3]

x = fromIntegral.sum lst
y = fromIntegral.length lst

z = x / y

Solution

  • You're getting tripped up by haskell's precedence rules for operators, which are confusing.

    When you write

    x = fromIntegral.sum lst
    

    Haskell sees that as the same as:

    x = fromIntegral.(sum lst)
    

    What you meant to write was:

    x = (fromIntegral.sum) lst