Rustang
Rustang

Reputation: 546

Reorder the Ord of chars in Haskell?

Is there anyway I can adjust the Ord (order) of regular characters in a Haskell program such that 'a' > 'b' ?

If not, do y'all have any suggestions on how I can best replicate this functionality? I tried Data magicLetters = 'b' | 'a' | 'c' deriving (Show, Ord), but didn't get very far with that syntax.

Upvotes: 3

Views: 175

Answers (1)

bereal
bereal

Reputation: 34292

Depending on why and how you're going to use it, you can try a wrapper-type:

newtype MyChar = MyChar Char

instance Eq MyChar where
    MyChar x == MyChar y = x == y

instance Ord MyChar where
    MyChar x <= MyChar y = x >= y   -- reverses the order

If you need a more specific logic, like a > b, but b < c, you may need some ordering table.

EDIT: As @leftaroundabout points out, the above already exists as Down

Upvotes: 10

Related Questions