【发布时间】:2019-04-27 23:54:01
【问题描述】:
我有以下简单的程序来查找列表的平均值。我知道我的错误与类型推断有关,但我无法纠正它。我能得到一个更正和一个简单的解释吗?
average :: Float
average= uncurry (/) . sumlen
sumlen ::[Int]-> (Int,Int)
sumlen = foldl f (0,0)
where f (s,n) x = (s+x,n+1)
错误是:
• Couldn't match expected type ‘Float’
with actual type ‘[Int] -> Int’
• Probable cause: ‘(.)’ is applied to too few arguments
In the expression: uncurry (/) . sumlen
In an equation for ‘average’: average = uncurry (/) . sumlen
【问题讨论】:
-
您要查找哪个列表的平均值?您是否希望
average成为一个函数,而不是Float? -
首先:
average不是Float。这是一个函数[Int] -> Float。另外,你应该在应用(/)时明确地将Ints 转换为Float
标签: haskell