PureScript Edition
A set of challenges for jump starting your understanding of monads.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
We can use the allPairs
function to do things like generate poker hands. In MCPrelude we have defined two lists called cardRanks
and cardSuits
. Try calling your allPairs
function on these:
allPairs cardRanks cardSuits == [(Tuple 2 "H"),(Tuple 2 "D"),(Tuple 2 "C"),(Tuple 2 "S"),(Tuple 3 "H"),(Tuple 3 "D"),(Tuple 3 "C"),(Tuple 3 "S"),(Tuple 4 "H"),(Tuple 4 "D"),(Tuple 4 "C"),(Tuple 4 "S"),(Tuple 5 "H"),(Tuple 5 "D"),(Tuple 5 "C"),(Tuple 5 "S")]
But this isn’t a very nice representation. We want a more concise representation of the card to show our user. If you were writing a real poker-related program, instead of using a tuple you would probably create a data type Card
. Do that now and then write a Show
instance for it that returns the more concise representation "2H"
, "2D"
, etc.
show (Card 2 "h") == "2h"
Now create a new function allCards
that does the same thing as your allPairs
function but uses your new Card
data type instead. It should have the following type signature:
allCards :: Array Int -> Array String -> Array Card
This function should do the same thing as allPairs
, but with more concise output. When you write this function, don’t implement it using your previous allPairs
function. Rewrite it.
show (allCards cardRanks cardSuits) == "[2H,2D,2C,2S,3H,3D,3C,3S,4H,4D,4C,4S,5H,5D,5C,5S]"