title: Generating Cartesian products in Haskell
body: <p>I am trying to generate all possible combinations of n numbers. For example if n = 3 I would want the following combinations:</p>
  
  <pre><code>(0,0,0), (0,0,1), (0,0,2)... (0,0,9), (0,1,0)... (9,9,9).
  </code></pre>
  
  <p>This <a href="https://stackoverflow.com/questions/27180196/generating-all-possible-combinations-of-numbers-in-a-triplet">post</a> describes how to do so for n = 3:</p>
  
  <pre><code>[(a,b,c) | m &lt;- [0..9], a &lt;- [0..m], b &lt;- [0..m], c &lt;- [0..m] ]
  </code></pre>
  
  <p>Or to avoid duplicates (i.e. multiple copies of the same n-uple):</p>
  
  <pre><code>let l = 9; in [(a,b,c) | m &lt;- [0..3*l],
                           a &lt;- [0..l], b &lt;- [0..l], c &lt;- [0..l],
                           a + b + c == m ]
  </code></pre>
  
  <p>However following the same pattern would become very silly very quickly for <code>n &gt; 3</code>. Say I wanted to find all of the combinations: <code>(a, b, c, d, e, f, g, h, i, j)</code>, etc.</p>
  
  <p>Can anyone point me in the right direction here? Ideally I'd rather not use a built in funtion as I am trying to learn Haskell and I would rather take the time to understand a peice of code than just use a package written by someone else. A tuple is not required, a list would also work.</p>