1
votes

I have created two objects in Haskell, mainly a trump card and the cards in hand.

trump = Card Spade Two
myhand = [Card Diamond Three, Card Spade Two]

where card is denoted by:

data Card = Card Suit Rank

I have written a function called retrieveSuit which retrieves the suit of the cards which are spades, diamonds,hearts etc..

retrieveSuit :: Card -> Suit
retrieveSuit (Card suit _) = suit

The problem I'm having is that I'm trying to write a function that compares both the suits of the trump and hand cards and see if the hand cards contain the suit of the trump card but I'm having issue mapping across both arguments of trump and myhand as I only know how to map one argument but not both.

checkTrump :: Card -> [Card] -> Bool
checkTrump trump myhand = (map retrieveSuit myhand) (retrieveSuit trump) if trump == myhand then True else False

when i run, i get the error

error: parse error on input `if'

Am i applying the function retrieveSuit correctly to both arguments of checkTrump? In this case, the program would output True since the cards in hand does contain a Spade which is the suit of the trump card as well.

Edit: Update for second scenario. Instead of returning true or false, if i were to return the card in hand that has the same suit as trump:

checkTrump :: Card -> [Card] -> Card
checkTrump trump myhand = filter ((cardSuit trump) `elem` (map cardSuit myhand))
1

1 Answers

2
votes

I think you're overcomplicating this:

checkTrump trump myhand = (retrieveSuit trump) `elem` (map retrieveSuit myhand)

(PS it would make more sense to me for the function to take just the suit, instead of the full card, for the trump argument. But the above should work for the function you are trying to make.)

For the second scenario, your function signature doesn't really make much sense because you have no guarantee that only one trump exists in the hand. There could be more than one - and there could also be none! So it makes most sense to me to change it to return a list, and just use the filter function (I also changed the name to avoid confusion, and to make it fit better with what the function does):

getTrumpsInHand :: Card -> [Card] -> [Card]
getTrumpsInHand trump hand = filter checkSuit hand
                                where checkSuit card = retrieveSuit card == retrieveSuit trump