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.

Combinations of more things

You probably noticed that allCombs3 is getting painful to write. Worse still, it does not help us generate combinations of four or more things. We need to take a different approach if we want this to scale easily. This is a difficult step, so we are not going to make you figure it out yourself. All the same, spend some time thinking about how you might do it. Here is a hint though. You probably noticed that allCombs has (Array a -> Array b) in its type signature and allCombs3 has (Array a -> Array b -> Array c) in its type signature. When you are playing with this, do not try to generalize that pattern to Array (Array a). That is not an adequate generalization because it only allows a and has no b or c anywhere. Spend some time thinking about this before you continue. But don’t be discouraged if you can’t figure it out. This one is hard.

Back? If you figured it out, congratulations. If not, here is the hex encoded type signature for a function called combStep that is the generalization we need.

636F6D6253746570203A3A20666F72616C6C206120622E204172726179202861202D3E206229202D3E2041727261792061202D3E2041727261792062

Now write that function. Then use the combStep function to implement allCombs and allCombs3 and check that the new implementations have the same behavior as before. Once you do this it should be pretty obvious how you would use combStep to implement allCombs4 and beyond. Also notice how combStep compares to allCombs.

Previous Page - Next Page