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.