[Haskell-beginners] decorate-sort-undecorate in haskell
Brandon S. Allbery KF8NH
allbery at ece.cmu.edu
Tue Jun 23 08:58:11 EDT 2009
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 at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university KF8NH
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20090623/d0874055/PGP.bin
More information about the Beginners
mailing list