我是Haskell的新手。
我对自定义数据类型很困惑
所以我要做的是显示一个人的总年龄以及每个人的姓名和年龄
所以我希望它看起来像这样
Detail "Ben" 20 Student
"Joy" 25 Police
"Hellen" 10 Student
Total 55 -- 20 + 25 + 10
这是我的代码
data Person = Name String Int Job deriving Show -- name age Job
data Det = Total Int | Detail String Int Job deriving Show -- age | name age Job
data Job = Police | Student deriving Show
Split :: Person -> Int -- Need to get eacn age to accumulate one another for total age
Split (Name _ age _ ) = age
check1 :: [Person] -> Det -- Need to sum all the age with Split function
......?
答案 0 :(得分:0)
total :: [Person] -> Int
total [] = 0
total (x:xs) = getAge x where
getAge (Name _ age _ ) = age + total xs
您需要使用“人员”类型的列表。这将返回列表中所有元素的总寿命。