I’m currently making a chess game for a school assignment and my pieces are defined
data Piece = Piece { piecetype :: PieceType, color :: PieceColor }
data PieceType = Pawn | Knight | Bishop | Rook | Queen | King
data PieceColor = Black | White deriving Eq
now I have to print a piece as a single char (king = k, queen = q, knight = n, etc.) for black pieces the value is a capital of its white value (king = K, queen = Q, knight = N, etc.)
So I made three instances of show
instance Show PieceColor where
show Black = "B"
show White = "W"
instance Show PieceType where
show Pawn = "P"
show Knight = "N"
show Bishop = "B"
show Rook = "R"
show Queen = "Q"
show King = "K"
and the third one is the problem
instance Show Piece where
show (piecetype, color) = show piecetype
if show color == "W"
then show piecetype
else toUpper (show piecetype)
I receive the following error (also I tried a lot more than this but according to this link I seem pretty close Something somewhat similar
Couldn't match expected type `Piece' with actual type `(t0, t1)'
I appreciate any help
Kind regards, Me
Direct Answer:
Explanations:
Pieceis a datatype with constructorPiece(both type name and constructor are same for your case) and not a tuple. So you should pattern match on constructor and not on tuple.toUpperandtoLowerare present inData.Charand work onCharwhile things like"B"areString(Stringis type synonym for[Char]). So to make aStringuppercase you can usemap toUpper.Comparing string like
show color == "B"is not very efficient instead you can use pattern matching on the constructor names.