All possible combinations of colors for Mastermind
You are looking for all of the variations (with repetitions) of a given list!

variations :: Int -&gt; [a] -&gt; [[a]]
variations n cs
 | n &lt;= 0 = []
 | n == 1 = map pure cs --  map (\c -&gt; [c]) cs
 | otherwise = variations (n-1) cs &gt;&gt;= \ps -&gt; map (:ps) cs


Where the right side of the last line could be any of the following:

variations (n-1) cs &gt;&gt;= flip map cs . flip (:)

concatMap (\ps -&gt; map (:ps) cs) (variations (n-1) cs)

concat . map (\ps -&gt; map (\c -&gt; c:ps) cs) $ variations (n-1) cs


Pick your poison. They are all equivalent. as &gt;&gt;= f is the same as concatMap f as, because for the list monad join is concat, and fmap is map, so bind = (&gt;&gt;=) is join . fmap = concat . map.

Let me demonstrate how variations is evaluated for cs = "12", n=2.

Since variations 1 "12" = ["1", "2"], we know that variations 2 "12" = concat . map (\ps -&gt; map (\c -&gt; c:ps) "12") $ ["1", "2"]  Let's see what each mapping does.

map (\c -&gt; c:ps) "12" simply takes a character c from "12" and appends it to the beginning of ps. But what's ps? Let's look at the other map.

The outer map simply applies the inner function \ps -&gt; map (\c -&gt; c:ps) "12" to each element of ["1","2"].
As we have seen, this inner function simply takes the list cs and maps the element c to c:ps. In this case it turns "1" into ["11","21"], and "2" into ["12","22"].

So all in all we have the following.

variations 2 "12" = concat . map (\ps -&gt; map (\c -&gt; c:ps) "12") $ variations 1 "12"
                  = concat . map (\ps -&gt; map (\c -&gt; c:ps) "12") $ ["1", "2"]
                  = concat [["11","21"],["12","22"]] 
                  = ["11","21","12","22"]


Hope this helps.