Merge list of tuples and list of integers

Viewed 107

I am new to Haskell and currently learning about lists.

I have a list of marks

[("Tom", 87, 78, 67), ("Dick", 56, 45, 72)]

and I need to get the sum of marks and the average of it. This is my approach.

Create a function to calculate list of sum of marks of each candidate

sumOfMarks = (\(a, b, c, d) -> b + c + d)

and map that function to the original list

newList = map sumOfMarks [("Tom", 87, 78, 67), ("Dick",  56, 45, 72)]

create a function to calculate list of average of marks of each candidate

avgList = map (`div`3) newList

merge all the lists original, newList and avgList

Desired output is

finalList = [("Tom", 87, 78, 67, 232, 77.34), ("Dick", 56, 45, 72, 173, 57.67)]

Another question is - whether this is the best approach to solving the problem and is there a better way?

2 Answers

(Leaving out the conversions necessary to use / instead of div)

we can combine the three lists using zipWith3, or using list comprehensions with zip3,

finalList =
 = zipWith3 (\ (n,a,b,c)  s  avg  ->  (n,a,b,c,s,avg)) 
               list  newList  avgList 
 = [ (n,a,b,c,s,avg) | ((n,a,b,c), (s, avg)) <- 
                          zip list 
                              (zip newList 
                                   avgList) ]
 = [ (n,a,b,c,s,avg) | ((n,a,b,c), s, avg) <- 
                          zip3 list 
                               (map sumOfMarks list)
                               (map (`div` 3) (map sumOfMarks list)) ]
 = [ (n,a,b,c,s,avg) | ((n,a,b,c), s, avg) <- 
                          zip3 list 
                               (map sumOfMarks list)
                               (map ((`div` 3) . sumOfMarks) list) ]
 = [ (n,a,b,c,a+b+c,(a+b+c)/3) 
                     | (n,a,b,c) <- list ]
 = [ (n,a,b,c,s,avg) 
                     | (n,a,b,c) <- list
                     , let s = a+b+c
                     , let avg = s/3 ]

List comprehensions are usually much more visually apparent (i.e. easier to use) than the various combinations of maps, zips, zipWiths, etc.

Related