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

  • Home
  • SEARCH
  • 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 5846033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:31:11+00:00 2026-05-22T12:31:11+00:00

LINQ library in .NET framework does have a very useful function called GroupBy ,

  • 0

LINQ library in .NET framework does have a very useful function called GroupBy, which I have been using all the time.
Its type in Haskell would look like

Ord b => (a-> b) -> [a] -> [(b, [a])]

Its purpose is to classify items based on the given classification function f into buckets, with each bucket containing similar items, that is (b, l) such that for any item x in l, f x == b.

Its performance in .NET is O(N) because it uses hash-tables, but in Haskell I am OK with O(N*log(N)).

I can’t find anything similar in standard Haskell libraries. Also, my implementation in terms of standard functions is somewhat bulky:

myGroupBy :: Ord k => (a -> k) -> [a] -> [(k, [a])]
myGroupBy f = map toFst
        . groupBy ((==) `on` fst) 
        . sortBy (comparing fst) 
        . map (\a -> (f a, a))
    where
        toFst l@((k,_):_) = (k, map snd l)

This is definitely not something I want to see amongst my problem-specific code.

My question is: how can I implement this function nicely exploiting standard libraries to their maximum?

Also, the seeming absence of such a standard function hints that it may rarely be needed by experienced Haskellers because they may know some better way. Is that true? What can be used to implement similar functionality in a better way?

Also, what would be the good name for it, considering groupBy is already taken? 🙂

  • 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-22T12:31:11+00:00Added an answer on May 22, 2026 at 12:31 pm

    Using Data.Map as the intermediate structure:

    import Control.Arrow ((&&&))
    import qualified Data.Map as M
    
    myGroupBy f = M.toList . M.fromListWith (++) . map (f &&& return)
    

    The map operation turns the input list into a list of keys paired with singleton lists containing the elements. M.fromListWith (++) turns this into a Data.Map, concatenating when two items have the same key, and M.toList gets the pairs back out again.

    Note that this reverses the lists, so adjust for that if necessary. It is also easy to replace return and (++) with other monoid-like operations if you for example only wanted the sum of the elements in each group.

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

Sidebar

Related Questions

I am using a ASP.NET MVC framework, jQuery library (1.3.2), linq to sql, sql
I have a class which uses both ADO.NET and LINQ to access a pair
LINQ simplifies database programming no doubt, but does it have a downside? Inline SQL
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.
The new IObservable/IObserver frameworks in the System.Reactive library coming in .NET 4.0 are very
does anyone know if there is a Twitter API library for the .Net Compact
I am trying to build the Json.NET 2.0 library to target the .NET framework
I am an asp.net web application developer and I always have used Enterprise Library
I'm using Scott Gu's dynamic linq library for Linq to XMl. The problem is
Possible Duplicate: 'Contains()' workaround using Linq to Entities? I am using the System.Linq.Dynamic library

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.