I ran into this problem whilst solving a puzzle, even though it isn’t the puzzle itself. Say you have some arbitrary set of numbers [1,2,3,4,5] and you want to consider any pair of two numbers in order of biggest sum first. I.e. (5,4), (5,3), (5,2), (4,3) and so on.
You can sort the original set – that’s ok – but > O(n^2) solutions like generating all pairs first and then sort them is intractable due to the size of the list.
I need a Haskell snippet which will lazily generate all such numbers given a list, in order of largest sum first. I’m working on it at the moment, but I thought it sufficiently interesting to share.
Any thoughts?