3 ms·
... which is butt-ugly compared to QuickSort in Haskell: qsort [] = [] qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)
by _pius 16y ago
... which is butt-ugly compared to QuickSort in Haskell:
qsort [] = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)
- ramchip 16y agoYes, and now try this: import Debug.Trace qsort [] = [] qsort (x:xs) = trace "(Called)" qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs) Prelude> qsort $! [1..100] Problem is, both of these "cute implementations" would be unusable in practice. It's hard to find beautiful code in 'real world' problems.
- _pius 16y agoWhile I see the problem with this implementation of QuickSort, I don't agree that "it's hard to find beautiful code in 'real world' problems." I think most programmers have opportunities to write elegant code every day.
- gruseom 16y agoInterestingly, this very passage of code is subjected to a withering critique in Alexandrescu's recent article on iteration: http://www.informit.com/articles/printerfriendly.aspx?p=1407357 http://www.informit.com/articles/printerfriendly.aspx?p=1407... (Not that recent, I guess. Nov 2009.)
- _pius 16y agoNice article, thanks!