简体   繁体   中英

Haskell: add the second elements of 2 tuples if their first elements are equal, in a list of tuples?

Examples:

addElement (1, 1) [(1, 1)] = [(1,2)]

addElement (2, 2) [(1,1),(2, 2)] = [(1,1),(2, 4)]

My code:

addElement :: (Integer, Integer) -> [(Integer, Integer)]  -> [(Integer, Integer)] 
addElement (a,b) ((x,y):xs)
                         | a==x = ((x,y+b):xs) 
                         |otherwise = addElement ((a+1), b)  xs

I couldn't make it work for the rest of the list.

You need to recurse on the function, and for the otherwise case yield the (x,y) 2-tuple. Furthermore you should implement the case with an empty list:

addElement :: (Eq a, Num b) => (a, b) -> [(a, b)]  -> [(a, b)]
addElement _ [] = []
addElement kv@(k1, v1) (kv2@(k2, v2):xs)
    | k1 == k2 = ((k2, v1+v2) : addElement kv xs
    | otherwise = kv2 : addElement kv xs

You can work with map to only implement logic that works on a single element, so:

addElement :: (Eq a, Num b) => (a, b) -> [(a, b)] -> [(a, b)]
addElement (k, v) = map f
    where f kv2@(k2, v2)
              | k == k2 = (k2, v+v2)
              | otherwise = kv2

One might also use a list comprehension to accomplish this:

addElem :: (Eq a, Num b) => (a, b) -> [(a, b)] -> [(a, b)]
addElem (x1, x2) lst = [if x1 == y1 then (y1, x2 + y2) else y | y@(y1, y2) <- lst]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM