So I have a function that takes a character and a list of tuples [tuple is a pair containing a character and an integer]. If the character matches the tuple character, the corresponding number is incremented by one. If not, then the tuple is printed as is.
So basically
foo 'a' [('a',1),('b',2),('c',1)]
Should yield
[('a',2),('b',2),('c',1)]
I have written the following code, but understandably, it skips printing over the last tuple:
foo a ((x,y):xs) | xs == [] = []
| x==a = (x,y+1):foo a xs
| x/=a = (x,y):foo a xs
foo 'a' [('a',1),('b',2),('c',1)]
O/P : [('a',2),('b',2)]
How do I modify the function such that it considers the last tuple too? I'd also like to be able to add a tuple with a count of 1 if the element is not present in the list of tuples