The Monad Challenges

PureScript Edition

A set of challenges for jump starting your understanding of monads.

Outline

Set 1: Random Numbers

Set 2: Failing Computations

Set 3: Combinations

Set 4: Common Abstraction

Set 5: Do Notation

MCPrelude documentation

This project is maintained by shaunplee and is a fork of the Monad Challenges maintained by mightybyte

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Do Notation – Set 1

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)

Previous Page - Next Page