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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:59:08+00:00 2026-06-18T19:59:08+00:00

I’m trying to write a function that accepts a list of comparators and returns

  • 0

I’m trying to write a function that accepts a list of comparators and returns a comparator that will compare a pair of values using the first comparator, then the second one if the first comparator returned EQ etc.

What I came up with was the following function:

import Data.Monoid

chainCompare :: [a -> a -> Ordering] -> a -> a -> Ordering
chainCompare = mconcat . map ($)

EDIT: chainCompare can also be written as (thanks to Vitus for pointing it out):

chaincompare = mconcat

An example of using this function is the following:

import Data.List
import Data.Ord

sortBy (chainCompare [comparing length, comparing sum]) [[1..100], [1..20], [100..200]]

However, this function requires using comparing explicitly, so I tried to modify the function like this:

chainCompare :: Ord b => [a -> b] -> a -> a -> Ordering
chainCompare = mconcat . map (comparing $)

However, chainCompare will cause a compile error in this case (Also, even if this example did compile, it would not work for empty strings):

 sortBy (chainCompare [length, head]) [['a'..'z'], ['A'..'Z'], "Lorem ipsum dolor sit amet"]

Is it possible to make chainCompare polymorphic in the sense that b can be any type of instance Ord? I have seen some Haskell code using the forall extensions and tried searching for them, but I still cannot figure out what each specific extension is useful for.

  • 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-06-18T19:59:09+00:00Added an answer on June 18, 2026 at 7:59 pm

    chainCompare [f, g] will always cause an error if f and g are functions with different types – no matter how you define chainCompare. You can even remove the chainCompare and just write [f, g] and it will still cause an error. The reason for that is that it is simply not possible to have values of different types in a list.

    Sometimes when you want to store values of different types in the same list it can make sense to use existential types (a GHC extension). With that you could define an existential type Comparator and use the list [Comparator length, Comparator head]. However that has no benefit over using comparing like you did in your first example, so it would be quite pointless in this case.

    So your first code using comparing is really the best you can do.

    For the record, here’s how the code would look like using existential types:

    {-# LANGUAGE ExistentialQuantification #-}
    
    import Data.Monoid
    import Data.Ord
    
    data Comparator a = forall b. Ord b => Comparator (a -> b)
    
    chainCompare :: [Comparator a] -> a -> a -> Ordering
    chainCompare = mconcat . map comp
      where comp (Comparator f) = comparing f
    
    -- Usage:
    list = [['a'..'z'], ['A'..'Z'], "Lorem ipsum dolor sit amet"]
    sortedList = sortBy (chainCompare [Comparator length, Comparator head]) list
    

    The only difference in usage over your first version is that you have to write Comparator instead of comparing and that it’s not possible to use a comparison function that does not compare based on a key. So as I said, it doesn’t really add any benefit over your first version.

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.