我是编程新手:/
我需要创建一个从3元组列表中检索第一个元素之和的函数。
我有类似的东西:
tuples = [(11,"11","11"),(22,"22","22"),(33,"33","33"),(44,"44","44"),(55,"55","55"),(66,"66","66")]
我需要列表中每个3元组的第一个元素的总和。 = 11 + 22 + 33 + 44 + 55
模式匹配可能吗?地图吗?
答案 0 :(得分:10)
将sum
与list comprehension:
sum [x | (x, _, _) <- tuples]
答案 1 :(得分:2)
如果您需要pointfree,可以尝试:
> let f = sum . map (\(x, _, _) -> x)
> f [(11,"11","11"),(22,"22","22"),(33,"33","33")]
66
注意:这有点x
,由于fst3 :: (a,b,c) -> a
内置缺乏我们无法避免