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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:22:22+00:00 2026-06-01T21:22:22+00:00

On the Haskell website , there’s this example quicksort implementation : quicksort :: Ord

  • 0

On the Haskell website, there’s this example quicksort implementation:

quicksort :: Ord a => [a] -> [a]
quicksort []     = []
quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
    where
        lesser  = filter (< p) xs
        greater = filter (>= p) xs

There is an explanation on the site, but I have a couple of questions that I didn’t see were addressed …

  • where is the actual comparison/swap done on two elements for a re-order? Is this handled by the ‘Ord’ (ordered) type definition itself. So the type enforces this condition of being ordered?
  • the ‘greater’ filter defines items ‘>= p’ (the pivot), so doesn’t this mean we’ll end up with an extra pivot [p] in resulting list of the function, due to the ‘++ [p]’ item?
  • 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-01T21:22:24+00:00Added an answer on June 1, 2026 at 9:22 pm

    where is the actual comparison/swap done on two elements for a
    re-order? Is this handled by the Ord (ordered) type definition
    itself. So the type enforces this condition of being ordered?

    What does Ord mean?

    Ord just means that a should be comparable with itself or in stricter terms operations such as >, <, and == should be defined for a. You can think of it as a constraint on the method.

    So, where is the ordering done?

    And the answer is the last pattern:

    quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)
        where
            lesser  = filter (< p) xs
            greater = filter (>= p) xs
    

    At run time, the program is going to get an array and the array must meet either of these two patterns:

    Pattern 1#: It is empty, in which case the function returns that same empty array and stops.

    Pattern 2#: It is not empty or in other words, there is a head element p appended to a tailing array xs. In such a case, the function is told to put p in the middle, put all elements of xs that are less than p on the left (as defined by lesser) of p and all elements of xs that are greater than or equal to p on the right of p. Furthermore, the function is finally told to apply itself (i.e., the same function quicksort) on lesser (which as we defined above, is the array on the left hand side of p) and greater (which as we defined above, is the array on the right hand side of p). As you can see, this will go on till you are left with a zero sized array and pattern 1# terminates the function.

    Finally, whenever those recursive calls terminate the function shall return the array:

    sortedlesser ++ p ++ sortedgreater 
    

    where sortedlesser is the array that resulted from the application of quicksort on lesser and sortedgreater is the array that resulted from the application of quicksort on greater.

    Wait… are we not duplicating p again and again?

    the ‘greater’ predicate defines items ‘>= p’ (the pivot), so doesn’t
    this mean we’ll end up with an extra pivot [p] in resulting list of
    the function, due to the ‘++ [p]’ item?

    No, this is not how pattern matching works. It is saying all elements in xs that are greater than or equal to p. By definition p itself is out of xs. If there are duplicates of p in xs then they will fall on the right hand side. Note that this choice will preserve the natural ordering of the original array.

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

Sidebar

Related Questions

Is there any website that lists and describes common type classes in Haskell?
Haskell's type safety is second to none only to dependently-typed languages. But there is
In Haskell, is there a way to restrict a monad M a so that
In Haskell, lifted type products mean that there's a semantic difference between (a,b,c) and
In Haskell, there is a function take n list which returns the first n
I know a Haskell module name, but I can't figure out in what package
Haskell is generally referenced as an example of a purely functional language. How can
In Haskell, Is there a standard library/package for generating Random / Arbitrary enums? I
In Haskell, is there a way to compare that all wildcards are of the
I'm currently learning Haskell using the Gentle Introduction to Haskell website , and I

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.