There is my code:
data Suit = Spade | Heart | Diamond | Club deriving (Eq, Show)
data CVal = Ace | King | Queen | Jack | Ten | Nine | Eight | Seven | Six | Five | Four | Three | Two deriving Show
data Card = Card Suit CVal deriving Show
sameSuit :: Card -> Card -> Bool
sameSuit (Card Spade _) (Card Spade _) = True
sameSuit (Card Heart _) (Card Heart _) = True
sameSuit (Card Diamond _) (Card Diamond _) = True
sameSuit (Card Club _) (Card Club _) = True
sameSuit (Card x _) (Card y _) = False
getNumber :: Card -> Int
getNumber (Card _ Two) = 2
getNumber (Card _ Three) = 3
getNumber (Card _ Four) = 4
getNumber (Card _ Five) = 5
getNumber (Card _ Six) = 6
getNumber (Card _ Seven) = 7
getNumber (Card _ Eight) = 8
getNumber (Card _ Nine) = 9
getNumber (Card _ Ten) = 10
getNumber (Card _ Jack) = 11
getNumber (Card _ Queen) = 12
getNumber (Card _ King) = 13
getNumber (Card _ Ace) = 14
beats :: Card -> Card -> Bool
beats x y = if sameSuit (x y) && getNumber(x) > getNumber(y) then True else False
Error message:
Couldn't match expected type `Bool' with actual type `Card -> Bool'
In the return type of a call of `sameSuit'
In the first argument of `(&&)', namely `sameSuit (x y)'
In the expression: sameSuit (x y) && getNumber (x) > getNumber (y)
I don't understand why i can't call function "sameSuit" in finction "beats". If I call it from prelude, like prelude > sameSuit (Card Club 10) (Card Club Ace) it returns right value and function type is Bool, not "Card -> Bool". What i do wrong? Can someone explain it to me?
if blahblahblah then True else False
can (and should) be shortened toblahblahblah
. – dave4420sameSuit
much simpler:sameSuit (Card x _) (Card y _) = x == y
. – augustssCVal
and deriveOrd
you don't need thegetNumber
function inbeats
, you could just usex > y
. – augustss