Haskell: Filter by fst and count by snd of pair -


i want define function pointcounts takes list of pairs in first member name , second point value , return list of pairs counted point each name.

i struggle days, cant figure how this.

the imput exmaple should like:

pointcount [("ferp",25),("herp",18),("derp",15),("ferp",25),("herp",15),("derp",18),("jon",10)]

and desired output example:

[("ferp",50),("herp",33),("derp",33),("jon",10)]  

here solution using nothing more exotic lists. cdk's solution simpler , has better running time.

import data.list  pointcount :: num => [(string, a)] -> [(string, a)] pointcount [] = [] pointcount ((x, n):xs) =     let (eqx, neqx) = partition ((==x).fst) xs in         (x, n + (sum$ map snd eqx)) : pointcount neqx 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -