Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reorder the Ord of chars in Haskell?

Tags:

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.

like image 472
Rustang Avatar asked Dec 23 '22 16:12

Rustang


1 Answers

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

like image 181
bereal Avatar answered Jan 01 '23 15:01

bereal