
On Jun 22, 2009, at 06:03 , Ivan Uemlianin wrote:
I'm learning Haskell from a background in Python, and I'm just looking at the sort and sortBy functions in Data.List. In Python, the decorate-sort-undecorate pattern is a popular alternative to using an explicit compare function. For example, to sort a list of lists by
It's fairly common, considering that decorate-sort-undecorate is a functional programming idiom dating back to Lisp. In Haskell it's usually expressed with the decoration in a tuple such that the default sort can be used.
map snd . sort . map (\x -> (x,decorate x))
Fancier versions use arrows to make the decorate part cleaner:
map snd . sort . map (decorate &&& id)
-- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH