I want to generate a list given a number n which returns all possible combinations from one to n recursively and without imports.
For example
generate 3
should return:
[[1,1,1],[1,1,2],[1,1,3],[1,2,1],[1,2,2],[1,2,3],[1,3,1],[1,3,2],[1,3,3],[2,1,1],[2,1,2],[2,1,3],[2,2,1],[2,2,2],[2,2,3],[2,3,1],[2,3,2],[2,3,3],[3,1,1],[3,1,2],[3,1,3],[3,2,1],[3,2,2],[3,2,3],[3,3,1],[3,3,2],[3,3,3]]
Logically something like this, which obviously returns an error:
generate n = [replicate n _ | _ <- [1..n]]
You can work with
replicateM :: Int -> m a -> m [a]:The
replicateMessentially works as:where we repeat
fxn times. Here we use[1 .. n], so that means we pick an element from[1 .. n], then another one for[1 .. n]and so on, until we pickednelements, and then combine these.