import - Haskell deriving additional instances for imported datatypes -
i'm relatively new haskell. write clone of card game uno , want pretty coloured output of card. do
import system.console.ansi
which provides
data color = black | red | green | yellow | blue | magenta | cyan | white deriving (bounded, enum, show)
now want add deriving (ord, eq) well, write in source file of imported package, there should easier way this. don't have clue keywords google or in book.
no need edit library. in source file, state:
instance eq color x == y = fromenum x == fromenum y instance ord color compare x y = compare (fromenum x) (fromenum y)
explanation: fromenum
function on enum
returns int
(black -> 0
, red -> 1
, etc.). integers equality-comparable , ordered.
edit: @rampion's version, in comments, prettier:
instance eq color (==) = (==) `on` fromenum instance ord color compare = compare `on` fromenum
Comments
Post a Comment