February 2019
Beginner to intermediate
180 pages
4h 4m
English
Now, we're ready to combine this with what we've learned from previous chapters. How would you write a program that creates a deck of cards, shuffles it, and draws the first five cards? Before looking at the following example, give it a shot yourself.
type suit = | Hearts | Diamonds | Spades | Clubs;type card = { suit, rank: int,};Belt.List.( makeBy(52, i => switch (i / 13, i mod 13) { | (0, rank) => {suit: Hearts, rank: rank + 1} | (1, rank) => {suit: Diamonds, rank: rank + 1} | (2, rank) => {suit: Spades, rank: rank + 1} | (3, rank) => {suit: Clubs, rank: rank + 1} | _ => assert(false) } ) ->shuffle ->take(5) ->Belt.Option.getExn ->( cards => { let rankToString = rank => switch (rank) { | 1 => "Ace" | 13 => "King" ...Read now
Unlock full access