Created
February 13, 2014 22:18
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to see what this looks like, you can run it (online) here: http://www.tryfsharp.org/Create