[<<] Industrie Toulouse

April 25, 2002

Alright. Here's the "Haskell" Quicksorter done in "quicksorting across the universe" as a Python one liner:
qsort = lambda x: x != [] and (qsort([ lt for lt in x[1:] if lt < x[0] ]) + [x[0]] + qsort([ gte for gte in x[1:] if gte >= x[0] ])) or x

In formatted mode:

qsort = lambda x: x != [] and \
           (qsort([ lt for lt in x[1:] if lt < x[0] ]) + 
            [x[0]] + 
            qsort([ gte for gte in x[1:] if gte >= x[0] ])
           ) or x

J. Shell, April 25, 2002 05:26 PM, in Python
Speaking of "Haskell", I once was able to turn the following Haskell example of a Quicksort (not an efficient Quicksort, just one showing Haskell's list comprehensions power) into a one line Python example, combining lambdas, list comprehensions, and good old binary logic.

qsort []     = []
qsort (x:xs) = qsort elts_lt_x ++ [x] ++ qsort elts_greq_x
                 where
                   elts_lt_x   = [y | y <- xs, y < x]
                   elts_greq_x = [y | y <- xs, y >= x]

I found a Python version at ActiveState's ASPN, but my version is much more fascinating.

I think it might have gotten lost in the move... I'll try to recreate it tonight.

J. Shell, April 25, 2002 01:13 PM, in
It's a shame that the Python PEP 289, Generator Comprehensions, has been rejected. I don't know why this idea appeals so much to me in the first place. Maybe it's a syntax thing. I love Python's functional programming side. I love List Comprehensions. I like generators. Why not put them together? Guido is apparently wary of turning Python into more of a functional language. A nice thing about FP in Python, so far, is that it's had decent support for FP constructs that don't have to be used if one doesn't wish to. I like that Python is so general purpose, but can offer many features found in more obscure and targeted languages like Haskell.
J. Shell, April 25, 2002 12:48 PM, in