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.

Generalizing Lists of Generators

By now you have probably realized that generating multiple random numbers this way is rather painful. You have to thread the output seed from one rand call to the input of the next call. This is tedious and error prone, so now you will create a function to make this a little easier.

repRandom :: forall a. Array (Gen a) -> Gen (Array a)

This function lets you give it a list of generators and it automatically handles the state threading for you.

The nice thing about this function is that Array (Gen a) is really general, so it composes well with other built-in list functions. For example:

repRandom (replicate 3 randLetter) (mkSeed 1)

This function should generate the same three letters that you got from randString3 in challenge #2.

Previous Page - Next Page