Generating sorted list of all possible coprimes <p>I need to generate infinite sorted list of all coprimes. The first element in each pair must be less than the second. The sorting must be done in ascending order -- by the sum of pair's elements; and if two sums are equal, then by the pair's first element.</p> <p>So, the resulting list must be </p> <pre><code>[(2,3),(2,5),(3,4),(3,5),(2,7),(4,5),(3,7),(2,9),(3,8),(4,7)...` </code></pre> <p>Here's my solution.</p> <pre><code>coprimes :: [(Int, Int)] coprimes = sortBy (\t1 t2 -&gt; if uncurry (+) t1 &lt;= uncurry (+) t2 then LT else GT) $ helper [2..] where helper xs = [(x,y) | x &lt;- xs, y &lt;- xs, x &lt; y, gcd x y == 1] </code></pre> <p>The problem is that I can't take <code>n</code> first pairs. I realize that sorting can't be done on infinite lists.</p> <p>But how can I generate the same sequence in a lazy way?</p>