Skip to content

Instantly share code, notes, and snippets.

@KushalP
Created February 13, 2014 22:18
Show Gist options
  • Save KushalP/8985098 to your computer and use it in GitHub Desktop.
Save KushalP/8985098 to your computer and use it in GitHub Desktop.
Playing around with F# to see how easy it is to use the type system to create a deck of cards
type Suit =
| Club
| Diamond
| Heart
| Spade
type Rank =
| Ace
| King
| Queen
| Jack
| Value of int
type Card = Card of Rank * Suit
let DeckOfCards = [
for s in [Club; Diamond; Heart; Spade] do
for r in [Ace; King; Queen; Jack] do
yield Card(r,s)
for v in 2..10 do
yield Card(Value v, s)
]
let shuffle cards =
let rand = new System.Random()
cards
|> List.map (fun c -> (rand.Next(), c))
|> List.sortBy fst
|> List.map snd
let ShuffledCards = shuffle DeckOfCards
@KushalP
Copy link
Author

KushalP commented Feb 13, 2014

If you want to see what this looks like, you can run it (online) here: http://www.tryfsharp.org/Create

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment