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 438235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:39:29+00:00 2026-05-12T20:39:29+00:00

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look

  • 0
group :: Ord a => [(a, [b])] -> [(a, [b])]

I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding the unnessecary pair and so on…

I got as far as:

group ((s, ls):(s', ls'):ps) = 
    if s == s' 
    then group ((s, ls++ls'):ps) 
    else (s, ls) : group ((s', ls'):ps)
group p = p

but obviously this ain’t going to cut it, because it doesn’t group everything.

Edit:
example

[("a", as),("c", cs), ("c", cs3), ("b", bs),("c", cs2), ("b", bs2)]

would output

[("a", as),("c", cs++cs2++cs3),("b", bs++bs2)]
  • 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-12T20:39:29+00:00Added an answer on May 12, 2026 at 8:39 pm

    Two alternative solutions to barkmadley’s answer:

    • As Tirpen notes in a comment, the best way to attack this problem depends on the number m of distinct first elements in the tuples of the input list. For small values of m barkmadley’s use of Data.List.partition is the way to go. For large values however, the algorithm’s complexity of O(n * m) is not so nice. In that case an O(n log n) sort of the input may turn out to be faster. Thus,

      import Data.List (groupBy, sortBy)
      combine :: (Ord a) => [(a, [b])] -> [(a, [b])]
      combine = map mergeGroup . myGroup . mySort
        where
          mySort = sortBy (\a b -> compare (fst a) (fst b))
          myGroup = groupBy (\a b -> fst a == fst b)
          mergeGroup ((a, b):xs) = (a, b ++ concatMap snd xs)
      

      This yields [("Dup",["2","3","1","5"]),("Non",["4"])] on barkmadley’s input.

    • Alternatively, we can call in the help of Data.Map:

      import Data.Map (assocs, fromListWith)
      combine :: (Ord a) => [(a, [b])] -> [(a, [b])]
      combine = assocs . fromListWith (++)
      

      This will yield [("Dup",["5","1","2","3"]),("Non",["4"])], which may or may not be an issue. If it is, then there are again two solutions:

      • Reverse the input first using Data.List.reverse:

        import Data.List (reverse)
        import Data.Map (assocs, fromListWith)
        combine :: (Ord a) => [(a, [b])] -> [(a, [b])]
        combine = assocs . fromListWith (++) . reverse
        
      • Prepend (flip (++)) instead of append ((++)) (Thanks to barkmadley; I like this solution better):

        import Data.Map (assocs, fromListWith)
        combine :: (Ord a) => [(a, [b])] -> [(a, [b])]
        combine = assocs . fromListWith (flip (++))
        

      Both of these definitions will cause combine to output [("Dup",["2","3","1","5"]),("Non",["4"])].

    As a last remark, note that all these definitions of combine require the first element of the tuples in the input list to be instances of class Ord. barkmadley’s implementation only requires these elements to be instances of Eq. Thus there exist inputs which can be handled by his code, but not by mine.

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

Sidebar

Related Questions

Our group needs to have a standard Common Look and Feel (CLF) for all
My group has a source analysis tool that enforces certain styles that we have
I want to have search results from SOLR ordered like this: All the documents
I'm trying to group by categories; let's say that I have two tables named
Group, I have a table that is dynamically built with device ids. I have
I want to use Data.List.groupBy to group a list of tuples based on the
My group is a Perl shop in an organization that is very heterogenous. Although
A group of developers that I am working with switched from VSS to SVN
I Group By all the deals in my database by store. How can I
Group, I have a quick question? -Is there a way to trigger this onclick

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.