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 8977167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:18:39+00:00 2026-06-15T19:18:39+00:00

I am working on an F# tutorial that creates a deck of cards. The

  • 0

I am working on an F# tutorial that creates a deck of cards. The types are listed, but I cannot understand how to loop through the types to create the map of the full deck. I expected to do something like

Foreach rank in ranks
   Foreach suit in suits
       somehow combine the two
   next suit
next rank

Is there no way to do this? Below are the types created.

I think if I changed them from types to lists they could union, right? So, what’s the point of types?

type suits=
    |Spade=1
    |Heart=2
    |Club=3
    |Diamond=4

type ranks=
    |ValCard of int
    |Jack 
    |Queen
    |King

type deck= Deck of ranks * suits
  • 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-15T19:18:40+00:00Added an answer on June 15, 2026 at 7:18 pm

    Enums is a good choice for representing cards. You have comparison among suits and among ranks for free, and easily convert enums from/to int.

    type suit =
        | Spade = 1
        | Heart = 2
        | Club = 3
        | Diamond = 4
    
    type rank = 
        | Ace = 1 | Two = 2 | Three = 3 | Four = 4 | Five = 5 | Six = 6 | Seven = 7 
        | Eight = 8 | Nine = 9 | Ten = 10 | Jack = 11 | Queen = 12 | King = 13
    
    /// 'Card' is a type which represents a particular card     
    type Card = Card of rank * suit
    
    /// 'deck' is a list consisting of all cards in a full deck
    let deck = [ for r in 1..13 do
                   for s in 1..4 do
                     yield Card(enum<rank> r, enum<suit> s) ]
    

    If you go for discriminated unions, you have to manually make lists of all suits and all ranks. The advantage is better pattern matching of DUs than that of enums.

    type suit =
        | Spade
        | Heart
        | Club
        | Diamond
    
    type rank = | Ace | Two | Three | Four | Five | Six | Seven 
                | Eight | Nine | Ten | Jack | Queen | King
    
    type Card = Card of rank * suit
    
    let private suits = [Spade; Heart; Club; Diamond]
    let private ranks = [Ace; Two; Three; Four; Five; Six; Seven; 
                         Eight; Nine; Ten; Jack; Queen; King]
    
    let deck = [ for rank in ranks do
                   for suit in suits do
                     yield Card(rank, suit) ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running through a tutorial that is a little dated, but I've tried to
I am a very green nubie, anyways, I am working through a tutorial that
I'm very new to backbone but I manage to get it working from tutorial.
I'm working through a tutorial right now on form validation, and I would like
I'm working on this tutorial , which shows how to create an app with
I am working through this tutorial https://developers.google.com/cloud-print/docs/android for google cloud print. I am trying
I am currently working through this tutorial: Getting Started with jQuery For the two
I have been working through the tutorial adapting it to a project I want
I'm new to Django but am working on the tutorial on the Django website
I am having no luck getting NLog to work. Working through the tutorial ,

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.