One line sort

Christian Gollwitzer auriocus at gmx.de
Tue Nov 16 02:15:38 EST 2021


Am 15.11.21 um 14:10 schrieb ast:
> A curiosity:
> 
> q = lambda x: x and q([i for i in x[1:] if i < x[0]]) + [x[0]] + q([i 
> for i in x[1:] if i >= x[0]])
> 
>  >>> q([7, 5, 9, 0])
> [0, 5, 7, 9]

That seems to be a translation of the classic Haskell quicksort example:

qsort [] = []
qsort (x:xs) = qsort [a | a <- xs, a < x]
                       ++ [x] ++
                qsort [b | b <- xs, b >= x]


The Haskell version is a bit clearer IMHO due to the pattern matching, 
but that results in a 2 liner :)

	Christian


More information about the Python-list mailing list