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.
First, create an instance of Monad
for your Gen
newtype, similar to what you did in Set 4. You will also probably want to create an evalGen :: forall a. Gen a -> Seed -> a
function as well.
With your Monad
instance, you should be able to use do syntax. Recall that in Set 1 we had a function called rand :: Seed -> (Tuple Seed Int)
. Create a new function makeRandom :: Gen Int
which wraps the rand
function inside your new type.
Next, use do syntax to re-create the following function from Set 1:
fiveRands :: Gen (Array Int)
To check that you created this function correctly, recall that the product of the five numbers you generate when passing in a seed of mkSeed 1
is 492745054.
Once that has been created correctly, create randLetter :: Gen Char
and use it to create randString3 :: Gen String
, which creates a String
of three random characters. If you have an initial seed of 1, when you use this site to calculate the SHA-256 hash of the output of randString3, you get 6b70649415be2833ee195809e02c7911a9be16b68fa07d10b4a644efa177798a.
Lastly, go ahead and create this general function:
generalPair :: forall a b. Gen a -> Gen b -> Gen (Tuple a b)