Late, here is a version that should not leak through space the same way (and seems to work about twice as fast as the other tripartite version here):
qsort3 xs = go xs [] where go (x:xs) zs = part x xs zs [] [] [] go [] zs = zs part x [] zs abc = go a ((x : b) ++ go c zs) part x (y:ys) zs abc = case compare yx of LT -> part x ys zs (y:a) bc EQ -> part x ys zs a (y:b) c GT -> part x ys zs ab (y:c)
This concerns a possible problem with using tuples, where let (a,b) = ... actually translated into let t= ...; a=fst t; b=snd t let t= ...; a=fst t; b=snd t let t= ...; a=fst t; b=snd t , which leads to a situation where even after a been consumed and processed, it is still supported around the living, as part of the tuple t , for b to be read from it - although, of course, it is completely unnecessary. This is called the Wadler pair leak problem. Or maybe GHC (with -O2 ) is smarter than that. :)
It also apparently uses a difference list approach (thanks, hammar), which also makes it more efficient (about twice as fast as the version using tuples). I think that part uses the parameters of the battery, as it builds them in the reverse order.
Will Ness Mar 03 '12 at 22:27 2012-03-03 22:27
source share