I want to generate a vectorspace from a basis pair, which looks something like:
genFromPair (e1, e2) = [x*e1 + y*e2 | x <- [0..], y <- [0..]]
When I examine the output though, it sems like I’m getting [0, e2, 2*e2,...] (i.e. x never gets above 0). Which sort of makes sense when I think about how I would write the code to do this list comprehension.
I wrote some code to take expanding “shells” from the origin (first the ints with norm 0, then with norm 1, then norm 2…) but this is kind of annoying and specific to Z^2 – I’d have to rewrite it for Z^3 or Z[i] etc. Is there a cleaner way of doing this?
The data-ordlist package has some functions which are extremely useful for working with sorted infinite lits. One of these is
mergeAllBy, which combines an infinite list of infinite lists using some comparison function.The idea is then to build an infinite list of lists such that
yis fixed in each list, whilexgrows. As long as we can guarantee that each list is sorted, and that the heads of the lists are sorted, according to our ordering, we get a merged sorted list back.Here’s a quick example:
Trying this in GHCi we get the expected result: