你好,我不明白为什么会有以下问题:
我正在尝试使用taxes
中的Company
实现为taxes
定义Employee
的定义。我不明白为什么会遇到以下错误:
data Employee=Employee{
age::Int,
name::String,
job::Job,
wage::Double
}
data Job= Worker | Manager |Unemployed deriving(Eq,Ord,Show)
data Company=Company{
compName::String,
year::Int,
employees::[Employee]
}
class Charges a where
taxes::a->Double
instance Charges Employee where
taxes Employee{age=a,wage=w}=fromIntegral a * w
实现1:
instance Charges Company where
taxes comp=foldl ((+).taxes) 0 employees
错误:
Couldn't match type `[Employee]' with `Double'
Expected type: Company -> Double
Actual type: Company -> [Employee]
为什么会出现问题,因为我一个接一个地Employee
申请了taxes
,而该Employee
已针对 instance Charges Company where
taxes comp =foldl ((+).taxes) 0 (employees comp)
实现,并且将其添加到计数器中?
实施2 -使用文件夹
Couldn't match expected type `Company -> Double'
with actual type `Double'
* Possible cause: `foldr' is applied to too many arguments
错误:
var sums = [ 0, 0, 0, 0, 0 ];
我看不到3个参数,这是什么问题?
答案 0 :(得分:6)
在Prelude中已经有一个功能非常适合对数字列表求和。为什么我们要重整总和?将问题分为几部分:
因此:
instance Charges Company where
taxes = sum . map taxes . employees