Long time, no blog post. Quite a few of my friends have
wasted no time in asking me if my blog is dead.
I’ve been trying to learn Haskell lately.
Found nothing to blog about, so I thought this might be interesting.
How would something I found on the haskell wiki translate to Python.
qsort :: (Ord a) => [a] -> [a] qsort  =  qsort (x:xs) = qsort([ n | n <- xs , n < x ]) ++ [x] ++ qsort([ n | n <- xs , n >= x ])
Sreeram came up with this:
def qsort(l): if not l : return l return qsort([x for x in l[1:] if x < l]) + [l] + qsort([x for x in l[1:] if x >= l])
head = lambda x: x tail = lambda x: x[1:] def qsort(l): return l if not l else qsort(filter(lambda x:x<head(l),tail(l))) + [head(l)] + qsort(filter(lambda x:x>=head(l),tail(l)))
I was thinking about avoiding IndexErrors, but I’d forgotten that the
function would never reach that condition, since “not l” will evaluate to True for an empty list
And also that head should return an element of the list, not a list
Finally, I also found this
Quicksort in 3 Lines recipe on the Python Cookbook
PS: Couldn’t come up with a title for this post, I was almost tempted to use `Nothing`,
Finally decided to come up with something more inflamatory.