Home > Software engineering >  Couldn't match expected type `(a1 -> a1 -> a1) -> (t0 a0 -> Int) -> t Int ->
Couldn't match expected type `(a1 -> a1 -> a1) -> (t0 a0 -> Int) -> t Int ->

Time:07-26

I was reading Programming in Haskell by Graham Hutton and was running some sample code that demonstrated how to average a list of integers. The function in question is

average ns = sum ns div length ns

The full error message is

Couldn't match expected type `(a1 -> a1 -> a1)
                                    -> (t0 a0 -> Int) -> t Int -> Int'
                  with actual type `Int'
    * The function `sum' is applied to four arguments,
      but its type `t Int -> Int' has only one
      In the expression: sum ns div length ns
      In an equation for `average': average ns = sum ns div length ns
    * Relevant bindings include
        ns :: t Int (bound at ch2-4.hs:7:9)
        average :: t Int -> Int (bound at ch2-4.hs:7:1)

I'm a bit new to the language so the error messages are quite confusing, even with reading up the documentation. Any help on debugging this is much appreciated.

I would also like to note that I'm aware running average ns = div (sum ns)(length ns) would work as well, but the function prior to it is more natural.

CodePudding user response:

Likely the code sample is:

average ns = sum ns `div` length ns

with backticks around the div. These backticks are used to use div as an operator, and is thus equivalent to div (sum ns) (length ns).

  • Related