Mario Grebelski
Mario Grebelski

Reputation: 37

How to retrieve Int from this data type in Haskell?

I have the following declaration of determining a score for cards in blackjack:

data Score x = Value Int | Blackjack | Bust deriving (Show, Ord, Eq)

which when passed into a function takes the type

Score Int

My question is if I have a Score Int how can I extract the Int from it?

Thanks in advance

Upvotes: 0

Views: 872

Answers (1)

Dan Robertson
Dan Robertson

Reputation: 4360

What you wrote:

data Score x = Value Int | Blackjack | Bust deriving (Show, Ord, Eq)

Has defined some datatype Score a where you Gett to choose what type a is. But you don’t use the type a at all. So let’s try a simpler type:

data Score = Value Int | Blackjack | Bust deriving (Show, Ord, Eq)

Now let’s write a score function. I’m not really sure what this should return so I’ll guess and you can change it:

score (Value x) = x
score Blackjack = 21
score Bust = 0

Or maybe you could do something else with this:

describe x = case x of
   Value x   -> "You did OK."
   Blackjack -> "You did great!"
   Bust      -> "You were just unlucky."

Upvotes: 3

Related Questions