 <p>The pure part of algorithm M is quite short indeed:</p> <pre><code>algorithmM = mapM (\n -&gt; [0..n-1]) </code></pre> <p>For example, here's a run in ghci:</p> <pre><code>&gt; algorithmM [2,3] [[0,0],[0,1],[0,2],[1,0],[1,1],[1,2]] </code></pre> <p>It's also quite easy to put an input/output loop around it. For example, we could add</p> <pre><code>main = readLn &gt;&gt;= mapM_ print . algorithmM </code></pre> <p>Compile and run a program containing these two (!) lines, and you will see something like this:</p> <pre><code>% ./test [2,3] [0,0] [0,1] [0,2] [1,0] [1,1] [1,2] </code></pre>