blueMountain
blueMountain

Reputation: 23

Implementation of quick sort using haskel

I was wondering why my quick sort code below shows the 'Unresolved top-level overloading error'

sort = \xs -> case xs of
                [] -> []
                y:ys -> sort[p | p <- ys, p < y]
                        ++ y:sort[p| p <- ys, p > y] 

Can you let me know why?

Upvotes: 0

Views: 59

Answers (1)

jf_
jf_

Reputation: 3479

The type of this function cannot be inferred, because there are multiple types possible with the constraint that < and > are used. You can fix this by adding a signature such as below (as general as possible). Btw your implementation deletes recurring elements. Using <= instead of <fixes this:

sort :: Ord a => [a] -> [a]
sort = \xs -> case xs of
                [] -> []
                y:ys -> sort[p | p <- ys, p <= y]
                        ++ y:sort[p| p <- ys, p > y] 

Example call (double 7's):

> sort [12,34,2,4,7,6,34,7,3]
[2,3,4,6,7,7,12,34,34]

Upvotes: 1

Related Questions