HPM Education - Haskell

Searchβ¦

Introduction

Types in Haskell

Defining Functions / Working with Functions

List Comprehensions

Higher-order Functions

Cutom Types

Interactive Programming

Functors, Applicatives and Monads

Exercise β Making a Card Deck Type

Let's now try to implement a data type that will represent a deck of cards. First, we can think about what type would be fitting for a card deck β a list of cards would be a good representation. But then what type is fitting for a single card? **Each card should have a rank and a suit** so we can make another type for cards that has the type of a tuple

`(Rank, Suit)`

. Let's start at the lowest level, the `Rank`

and `Suit`

type, and remember that we already defined the `Suit`

type, but we will now also derive the `Show`

class for it:1

data Suit = Hearts

2

| Diamonds

3

| Spades

4

| Clubs

5

deriving (Show)

Copied!

which leaves us with the task of defining

`Rank`

for which we can also use nullary constructors and also derive some built-in classes:1

data Rank = Deuce

2

| Three

3

| Four

4

| Five

5

| Six

6

| Seven

7

| Nine

8

| Ten

9

| Jack

10

| Queen

11

| King

12

| Ace

13

deriving (Show, Eq, Ord, Enum)

Copied!

We can then already use methods of those classes on the

`Rank`

type:1

ghci> Deuce < Three

2

True

3

β

4

ghci> Deuce <= Three

5

True

6

β

7

ghci> Deuce == Three

8

False

9

β

10

ghci> Deuce > Three

11

False

12

β

13

ghci> [Deuce .. Five]

14

[Deuce, Three, Four, Five]

Copied!

We have the

`Rank`

and `Suit`

types now, and we can simply define the type of Card as a type synonym for a tuple of `(Rank, Suit)`

:1

type Card = (Rank, Suit)

Copied!

Similarly, we can define a deck of cards as a type synonym for a list of

`Card`

types:1

type Deck = [Card]

Copied!

We have all the required types for actually building a deck now, so let's make a function for that purpose. We will use list comprehension and take advantage of the fact that

`Rank`

supports enumeration:1

buildDeck :: Deck

2

buildDeck = [(rank, suit) | rank <- [Deuce .. Ace], suit <- suitList]

3

where

4

suitList = [Hearts, Diamonds, Spades, Clubs]

Copied!

And we can now build a deck of cards using that function:

1

ghci> deck = buildDeck

2

ghci> show deck

3

"[(Deuce, Hearts),(Deuce, Diamonds),(Deuce, Spades),(Deuce, Clubs),

4

(Three, Hearts),(Three, Diamonds),(Three, Spades),(Three, Clubs),(Four,

5

Hearts),(Four, Diamonds),(Four, Spades),(Four, Clubs),(Five, Hearts),

6

(Five, Diamonds),(Five, Spades),(Five, Clubs),(Six, Hearts),(Six,

7

Diamonds),(Six, Spades),(Six, Clubs),(Seven, Hearts),(Seven, Diamonds),

8

(Seven, Spades),(Seven, Clubs),(Nine, Hearts),(Nine, Diamonds),(Nine,

9

Spades),(Nine, Clubs),(Ten, Hearts),(Ten, Diamonds),(Ten, Spades),(Ten,

10

Clubs),(Jack, Hearts),(Jack, Diamonds),(Jack, Spades),(Jack, Clubs),

11

(Queen, Hearts),(Queen, Diamonds),(Queen, Spades),(Queen, Clubs),(King,

12

Hearts),(King, Diamonds),(King, Spades),(King, Clubs),(Ace, Hearts),(Ace,

13

Diamonds),(Ace, Spades),(Ace, Clubs)]"

Copied!

β

Last modified 1yr ago

Copy link