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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:29:20+00:00 2026-06-18T07:29:20+00:00

The term general (contrary to specialized ) in the question means the function can

  • 0

The term general (contrary to specialized) in the question means the function can sort the items as long as they are of a type that is an instance of Ord.

Consider one of the most famous haskell ads

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

The above implementation is not in-place.

I was trying to write an in-place version.
It’s easy to make quicksort in-place. Usually, we just need a mutable array and I chose Foreign.Marshal.Array.
My implementation is in-place and runs very well, but I am not satisfied with its type signature

(Ord a, Storable a) => [a] -> IO [a]

To be more precise, the type constraint Storable a annoyed me.

Obviously, if we want to sort items, Ord constraint is needed, while Storable is unnecessary.
In contrast, the type signatures of the classic quicksort or sort in Data.List, is Ord a => [a] -> [a]. The constraint is just Ord.

I didn’t find a way to get rid of the additional constraint.

I searched Stackoverflow, and found some questions about in-place quicksort in haskell, e.g.

How do you do an in-place quicksort in Haskell
Why is the minimalist, example Haskell quicksort not a "true" quicksort?

Unfortunately, their major concern is just in-place. All of the in-place quicksort examples given there have additional type constraints as well.
For example, iqsort given by klapaucius has the type signature

iqsort :: (Vector v a, Ord a) => v a -> v a

Does anyone know how to implement an in-place quicksort haskell function with type signature Ord a => [a] -> [a]?
I know how to make an in-place quicksort, but I don’t know how to make it general.

  • 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-18T07:29:21+00:00Added an answer on June 18, 2026 at 7:29 am

    Yes it is possible. (Although in Haskell you want to use this kind of imperative algorithms only in cases where you really need top performance.)

    I know of 2 such algorithms:

    • sort from vector-algorithms.
    • qsort (or introsort) from marray-sort, which I haven’t released to Hackage yet. (Let me know if you need it.) It works on mutable arrays.

    (Introsort is basically refined quicksort that has O(n log n) worst case complexity.)

    I’m not sure about MVector, but for MArrays, you don’t have to worry about the additional constraints MArray a e m. They’re there to make the type more general, not less. Signatures like

    qsort :: (MArray a e m, Ord e) => a Int e -> m ()
    

    allow to use the same algorithm for different array representations. For some data types, you can have specialized arrays of that type which are faster and more compact than generic arrays. For example, if you want to sort 8-bit integers, there is a specialized instance MArray IOUArray Int8 IO for unboxed arrays. And a specialization of qsort for this kind of arrays just using polymorphism is

    qsort :: IOUArray Int Int8 -> IO ()
    

    But you also have instance MArray IOArray e IO that works arbitrary e. By using qsort with IOArray, you get a specialization without constraints on e:

    qsort :: (Ord e) => IOArray Int e -> IO ()
    

    Furthermore, if you use STArrays and the ST monad, you can sort an array in-place using the same function, and get the result later as a pure value, without IO.

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

Sidebar

Related Questions

Very simple question. What does the term 'seeding' mean in general? I'll put the
Is there an accepted general term that subsumes the concepts of variables, class instances
This is a very general question. I am a bit confused with the term
I'm wondering if there's a general term used for objects that map exactly to
How do you type the following general term of a series in Maxima ?
Is there a general term for a pairing of tables where one has header
A term that I see every now and then is Cyclomatic Complexity. Here on
The Term::Size-module jumbles up the encoding. How can I fix this? #!/usr/bin/env perl use
God I hate the term code smell, but I can't think of anything more
'Background processing' is a terrible term, but I can't think of a better one

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.