Second Update:
I finally followed zurgl's suggestion and wrote something that traverses and groups recursively. Thanks to everyone who was trying to help!
First Update:
What I hoped could be a sorting function, but was unsure about, was meant to optimize a grouping that would follow (minimizing the number of groups). The grouping collects tuples that are adjacent horizontally or vertically:
f xs =
foldr (\a@(y,x) ((b@(y',x'):xs):bs) -> if (y == y' && abs (x-x') == 1) ||
(x == x' && abs (y-y') == 1)
then (a:b:xs):bs
else [a]:(b:xs):bs)
[[last xs]] (init xs)
Output of the second example, after the "sorting":
*Main> f [(0,1),(1,1),(2,1),(2,2),(2,3),(1,3),(0,3),(4,1),(4,2)]
[[(0,1),(1,1),(2,1),(2,2),(2,3),(1,3),(0,3)],[(4,1),(4,2)]]
--end of updates--
I'm having trouble conceiving of a sorting function and am hoping someone might have an idea about how to implement it or perhaps say if more might be needed than a custom sort. I've tried toying with sortBy but do not seem to be making much progress.
How do I get from this:
[(0,1),(0,3),(1,1),(1,3),(2,0),(2,1),(2,3),(4,1),(4,2)]
to this:
[(0,1),(1,1),(2,0),(2,1),(0,3),(1,3),(2,3),(4,1),(4,2)]
Differences of 0 or 1 between both y y' and x x' should be primary. Does that make sense?
Second example:
[(0,1),(0,3),(1,1),(1,3),(2,1),(2,2),(2,3),(4,1),(4,2)]
=>
[(0,1),(1,1),(2,1),(2,2),(2,3),(1,3),(0,3),(4,1),(4,2)]