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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:37:05+00:00 2026-05-16T20:37:05+00:00

So i am given this intCMP :: Int -> Int -> Ordering intCMP a

  • 0

So i am given this

intCMP :: Int -> Int -> Ordering
intCMP a b | a == b = EQ
     | a < b = LT
     | otherwise = GT

intCMPRev :: Int -> Int -> Ordering
intCMPRev a b | a == b = EQ
     | a < b = GT
        | otherwise = LT

floatCMP :: Float -> Float -> Ordering
floatCMP a b | a == b = EQ
       | a < b = LT
       | otherwise = GT

I need to write this function

sort3 :: Ord a => (a -> a-> Ordering) -> [a] -> [a]
sort3 cmp xs =

Which will sort 3 or less elements by comparison. No recursion. I was wondering how this works as far as passing say intCMP. Why would you pass that into the sort function? Does it serve a purpose when sorting and returning the sorted list? I’m not really sure how to do the comparisons manually like that without any sort of recursive call, so I’m just trying to understand it better.

I was thinking of doing 3 comparisons and then moving the element to a certain position in the list, but i really don’t know how i could even do this in haskell. Any clues on how to start this would be great. Maybe some sort of pattern?

Thanks.

  • 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-16T20:37:06+00:00Added an answer on May 16, 2026 at 8:37 pm

    Partial answer to the first part of your question:

    Passing in intCMP to sort3 lets you control the way the sorting is done. Presumably, sort3 intCMP [7,3,6] will return [3,6,7], whereas sort3 intCMPRev [7,3,6] will return [7,6,3]. You could even make your own weird sorting functions like first all the even numbers and then all the odd ones in descending order, like so:

    intCMPWeird :: Int -> Int -> Ordering
    intCMPWeird a b
        | even a && even b = intCMP a b -- even numbers compare as usual
        | odd a  && odd b  = intCMPRev a b  -- odd numbers compare in reverse order
        | even a && odd b  = LT -- evens are all 'smaller' than odds
        | odd a  && even b = GT -- odds are all 'greater' than evens
    

    Using this, sort3 intCMPWeird [7,3,6] should give [6,7,3].

    What happens during typechecking when you pass one of the intCMP... functions into sort3 is (in a very small nutshell) that the compiler tries to match the type of sort3‘s first argument (a -> a -> Ordering) with the type of the supplied value (Int -> Int -> Ordering) and succeeds in doing that, by making a equal to Int. Then it needs to check whether the constraint Ord a is satisfied for Int, which works! Finally the compiler can figure out that the type of sort3 intCMP is [Int] -> [Int]. Similarly, sort3 floatCMP has type [Float] -> [Float].

    I’m not 100% sure why the Ord constraint is on the type of sort3, since you can get the needed information from the passed-in comparison function – are you sure you’ve typed that correctly?

    EDIT: Hint on how to use a where clause to get a readable definition, you still have to fill in some bits and add some more clauses:

    sort3 cmp [x,y,z] 
        | x <= y && somethingElse1 = listWithXyzInSomeOrder1
        | x <= z && somethingElse2 = listWithXyzInSomeOrder2
            where a <= b = cmp a b /= GT
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this plain JavaScript construct: var MyObject = function() { var privateArray = [
Given this simplified snippet : <html> <body> <div> <div style='text-align:center;'>asdfaskjdfakjsd</div> <div style='float:right'> <input type='submit'
Given this pseudo code of a function f(0) = 1; f(1) = 3; f(n)
Given this: var p = function(){}; p.prototype = { id : null, arr :
Given this example taken from http://php.net/manual/en/function.crypt.php crypt('rasmuslerdorf', '$2a$07$usesomesillystringforsalt$') Firstly: What is the length that
Given this Java code: int fst = 5; int snd = 6; if(fst ==
Given this code (from my last post here ): const int j = 5;
Given this ugly method: public function convert_cell_value( $val, $type ) { if($type == 'String')
Given this bit of code: var SuperClass = function(a, b) { this.a = a;
Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;

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.