简体   繁体   中英

Couldn't match expected type `(a1 -> a1 -> a1) -> (t0 a0 -> Int) -> t Int -> Int' with actual type `Int'

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM