07.26.07

Exercise 5.7

Posted in Uncategorized at 11:08 pm by admin

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

1 Comment »

  1. Wieslaw Poszewiecki said,

    April 12, 2008 at 9:52 am

    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)

Leave a Comment