Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9074871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:44:03+00:00 2026-06-16T18:44:03+00:00

I am learning Haskell, so I’m writing some simple card games. I’ve defined some

  • 0

I am learning Haskell, so I’m writing some simple card games. I’ve defined some data types:

data Rank = Ace|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Jack|Queen|King deriving (Eq,Show,Ord)

data Suit = Hearts|Spades|Diamonds|Clubs deriving (Show)

data Card = Card Rank Suit 

Now I’d like to create a pristine deck of 52 cards. I’m sure there is a slick way to do it, but all I can come up with is:

 pristineDeck = [Card Ace Hearts, Card Two Hearts, ...]

Can I get Haskell to generate this list for me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:44:04+00:00Added an answer on June 16, 2026 at 6:44 pm

    List comprehensions are a very tidy syntax for this. If you derive Enum on Rank and Suit you can express it very simply as:

    pristineDeck = [ Card rank suit | suit <- [Hearts .. Clubs], rank <- [Ace .. King] ]
    

    If you’re wondering why I have suit and rank in different orders, the first is because of the order the Card constructor uses, while the latter is to get the order of the resulting list–suits together in ascending order.

    In more generality, or when a single list comprehension gets too bulky, the cartesian product is exactly the behavior given by the Monad instance for lists. The following is equivalent to the list comprehension above:

    pristineDeck = do suit <- [Hearts .. Clubs]
                      rank <- [Ace .. King]
                      return $ Card rank suit
    

    As one other minor point, to save yourself the trouble of remembering what order the Suit values are in, deriving Bounded as well will enable to write [minBound .. maxBound] to enumerate all values of any type with instances of both Enum and Bounded.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning haskell and decided to try writing some small test programs to get
I'm learning Haskell and I'd like to write some multithreaded programs now to see
I have two programs to find prime numbers (just an exercise, I'm learning Haskell).
I'm learning Haskell, and one of my practice functions was a simple recursive permute
I'm learning Haskell and solving some programming problems on spoj.pl. The idea of the
I'm just learning Haskell and I am trying to write some code that simply
Still learning haskell, and I cannot really see the difference between data Tree a
I'm learning haskell, and my current project is writing a parser to read a
I've been learning Haskell for some time now and with every new programming language
I'm working on learning some Haskell (please excuse the newbie error)- This routine errors

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.