The Monad Challenges

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

This project is 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 :: [Gen a] -> Gen [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 [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