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 :: 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 -> (Integer, Seed)
. Create a new function makeRandom :: Gen Integer
which wraps the rand
function inside your new type.
Next, use do syntax to re-create the following function from Set 1:
fiveRands :: Gen [Integer]
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 8681089573064486461641871805074254223660.
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 9d475eb78d3e38085220ed6ebde9d8f7d26540bb1c8f9382479c3acd4c8c94a3.
Lastly, go ahead and create this general function:
generalPair :: Gen a -> Gen b -> Gen (a, b)