[Haskell-beginners] decorate-sort-undecorate in haskell

Henk-Jan van Tuyl hjgtuyl at chello.nl
Tue Jun 23 09:42:59 EDT 2009


On Tue, 23 Jun 2009 14:58:11 +0200, Brandon S. Allbery KF8NH  
<allbery at ece.cmu.edu> wrote:

> 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)
>

The simplest form for e.g. sorting by length is:
> sortByLength = sortBy (comparing length)

-- 
Met vriendelijke groet,
Henk-Jan van Tuyl


--
http://functor.bamikanarie.com
http://Van.Tuyl.eu/
--




More information about the Beginners mailing list