We’ve seen `zip`, but I don’t think `unzip` has been mentioned. To me it’s the obvious way to solve this problem.

addPairsPointwise :: [(Int, Int)] -> (Int, Int)
addPairsPointwise xs = (foldl (+) 0 as, foldl (+) 0 bs)
where (as,bs) = unzip xs
addPairsPointwise' xs = addPairsOp xs (0,0)
where addPairsOp ((x,y):xs) (a,b) = addPairsOp xs (a+x, b+y)
addPairsOp [] r = r |

addPairsPointwise :: [(Int, Int)] -> (Int, Int)
addPairsPointwise xs = (foldl (+) 0 as, foldl (+) 0 bs)
where (as,bs) = unzip xs
addPairsPointwise' xs = addPairsOp xs (0,0)
where addPairsOp ((x,y):xs) (a,b) = addPairsOp xs (a+x, b+y)
addPairsOp [] r = r

This entry was posted on Thursday, July 26th, 2007 at 11:08 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Another solution:

addPairsPointwise = foldl (\(x1,y1) (x2,y2)-> (x1+x2,y1+y2)) (0,0)

or without lambdas

addPairsPointwise’ = foldl pairAdd (0,0)

where pairAdd (x1,y1) (x2,y2) = (x1+x2,y1+y2)