我能够将所有元组的第z个元素相加,但由于一些奇怪的原因,我将结果除以2后得到1.
averageYear :: [(String, String, Int)] -> Int averageYear [] = 0 averageYear ((x,y,z):xs) = (z + (averageYear xs)) `div` (length xs)
谢谢!!
数学是完全错误的.
averageYear [(_, _, 2010)] = (2010 + averageYear []) `div` 2 = (2010 + 0) `div` 2 = 2010 `div` 2 = 1005
要计算平均值,您必须将总和除以项目数.
average :: [Int] -> Int average xs = sum xs `div` length xs averageYear :: [(String, String, Int)] -> Int averageYear = average . map (\(_, _, year) -> year)