为什么不是这个函数计算整数列表的平均值不工作?
问题描述:
我试图做一个函数,它从一个给定的名单加起来所有的数字,然后由6为什么不是这个函数计算整数列表的平均值不工作?
average :: [Integer] -> Integer
average m = (sum m) quot 6
分频,但是,这是我收到错误消息:
Couldn't match type `Integer'
with `(a0 -> a0 -> a0) -> a1 -> Integer'
Expected type: [(a0 -> a0 -> a0) -> a1 -> Integer]
Actual type: [Integer]
In the first argument of `sum', namely `m'
In the expression: (sum m) quot 6
答
你需要反引号周围quot
,或写它首先
答
在Haskell我们的论点之前写的函数名。对于QUOT你写:
你的情况quot 17 2
所以:
quot (sum m) 6
Haskell有一些sytactic糖,让你写函数在所谓缀符号。这是用户mb14引用的内容。
'(sum m)quot 6''意为“take'sum'并将其应用于'm',然后将得到的函数应用到'quot',然后将得到的函数应用到'6 '”。它与“总和”6“相同。但是,sum m不会返回函数,所以会引发类型错误。 – chi