F# Types and Looping

f#, loops, types

Solution

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 `suit`s and all `rank`s. 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) ]

Problem

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 ```

Original source