Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7049417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:00:43+00:00 2026-05-28T03:00:43+00:00

I want to make a function that takes a list eg [(‘A’,3), (‘B’,2), (‘C’,2),

  • 0

I want to make a function that takes a list eg

[('A',3), ('B',2), ('C',2), ('A',5), ('C',3), ('C',2)]

and then adds the numbers together when the letter is the same. So the above input would produce

[('A',8), ('B',2), ('C',7)]. 

Could someone please just give me an idea of how to approach this, I’d like to try and do as much as possible myself!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:00:43+00:00Added an answer on May 28, 2026 at 3:00 am

    You can use Data.Map‘s fromListWith to construct a map that has the summed values. It’s type is this:

    fromListWith :: Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
    

    It takes a list of pairs (as you described) as the first arg, if it sees a duplicate, it uses some function (the first arg), to combine the original value with a new one. From there, you can convert this map back into a list of pairs (also a function in Data.Map).

    You could do this with pure lists, but it probably won’t be that effecient, as you’ll be constructing a new list (or portions of it) quite often.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a function that iterates through a list, but takes two
I want to make a function that take month and year and return List<DateTime>
I want to make a function that is always running in xcode similar to
Let's imagine I want to make a templated function that returns the first element
I want to make sure that a set of functions have the same signature
I have a generic dispatcher that takes in parameters and then make $.ajax() As
I have this function below that takes a list of id's and searches the
I often want to make a multiline function call and reduce it down to
I want to make a link call a Javascript function through the onclick event
I want to make some C++ program and I'm using function popen here to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.