haskell sorting list of custom datatypes based on common atributes without usingf modules

101 Views Asked by At

Im trying to order a list of a custom datatype with different size of atributes to validate if the datatype format is true but I don't know how to manipulate it by using quick sort.

The datatype:

data Cuidado = Comprar String Int | Medicar String

I'm testing pattern matching with quicksort but it doesnt make any sense.

valCui :: [Cuidado] -> [Cuidado]
valCui [] = []
valCui (x:xs) = valCui [a | a <- xs, x > a] ++ [x] ++ valCui [a | a <- xs, x <= a]

Output is supposed to be:

valCui [Medicar med7, Comprar med4 30] == [Comprar med4 30, Medicar med7]
1

There are 1 best solutions below

4
leftaroundabout On

There are only two small problems here:

  1. You have a -> operator in your list comprehension. Presumably you mean to check the greater-than relation here, but the operator for that is just >. The -> arrow is special syntax, reserved for function signatures, lambdas and case assignments.

  2. In order to be able to use such comparison operators on your custom data type, you need it to have an instance of the Ord typeclass. But yours doesn't, or at least you didn't show it. The easiest way to get such an instance is to derive it:

    data Cuidado = Comprar String Int | Medicar String
     deriving (Eq, Ord)