PureScript Edition
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.
Have you ever needed to generate the Cartesian product of some things? Let’s use the term combination to mean an element from the Cartesian product. In imperative languages generating combinations always seemed like a pain to me. We are going to explore how PureScript can make combination generation easier.
First, write this function:
allPairs :: forall a b. Array a -> Array b -> Array (Tuple a b)
This function should generate all possible pairs of items from each of the two input lists. This means it should NOT have this behavior:
allPairs [1,2,3] [4,5,6] == [(Tuple 1 4),(Tuple 2 5),(Tuple 3 6)]
That’s the zip
function and it’s not what we are looking for here. Instead, your function should generate this:
allPairs [1,2] [3,4] == [(Tuple 1 3),(Tuple 1 4),(Tuple 2 3),(Tuple 2 4)]
Here is another test case:
allPairs (1..3) (6..8) == [(Tuple 1 6),(Tuple 1 7),(Tuple 1 8),(Tuple 2 6),(Tuple 2 7),(Tuple 2 8),(Tuple 3 6),(Tuple 3 7),(Tuple 3 8)]
Note that because of the way we set up the project template you cannot use list comprehensions to solve this. That’s intentional. You should solve it with explicit recursion.