Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Sorting (Erlend Hamberg)
   2. Re:  Sorting (mike h)


----------------------------------------------------------------------

Message: 1
Date: Tue, 13 Dec 2016 16:01:41 +0000
From: Erlend Hamberg <erl...@hamberg.no>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Sorting
Message-ID:
        <ca+g9oxn3n3+rpfjekhxfqsb_jxowk+5sooejcjndarxebnm...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

There is a really nice solution that takes advantage of Ordering's Monoid
instance (see https://wiki.haskell.org/Monoid).

The imports you need:

import Data.List (sortBy)
import Data.Ord (Down(..), comparing)
import Data.Monoid ((<>)) -- the “mappend” operator

You can then combine two calls to `comparing`

sortBy (comparing  (Down . snd) <> comparing fst) xs

(`Down` is just a newtype that reverses the ordering, since you wanted the
first element in descending order and the second in ascending order.)

On Tue, 13 Dec 2016 at 16:30 Francesco Ariis <fa...@ariis.it> wrote:

> On Tue, Dec 13, 2016 at 02:36:39PM +0000, mike h wrote:
> > Hi,
> >
> > I’m trying to sort a list of tuples. A char and a count of that char
> (Char , Int)
> > e.g.
> >
> > [ ('r',2), ('c',2),('a', 2), ('b',3), ('f',2)]
> >
> > e.g. ‘r’ occurs twice etc.
> > The order should be based on the count first and then ties broken by the
> > natural ordering of char.
>
> You should provide sortBy with an appropriate compare function, e.g.
>
>     comp (a,b) (c,d) | a > c = GT
>                      | -- etc etc.
>
> or go with the manky but working hack:
>
> λ> :m Data.List
> λ> sortOn (\(a, b) -> b*(-100) + fromEnum a) [('r',2), ('c',2),('a', 2),
> ('b',3), ('f',2)]
> [('b',3),('a',2),('c',2),('f',2),('r',2)]
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-- 
Erlend Hamberg
erl...@hamberg.no
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20161213/8b0b6af2/attachment-0001.html>

------------------------------

Message: 2
Date: Tue, 13 Dec 2016 16:15:18 +0000
From: mike h <mike_k_hough...@yahoo.co.uk>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Sorting
Message-ID: <877cdeb9-e4a5-4daa-b43e-c44bb1e63...@yahoo.co.uk>
Content-Type: text/plain; charset="utf-8"

Thanks folks.

Francesco
Cool - I just came to that conclusion too and did

tupleOrder :: (Char, Int) -> (Char, Int) -> Ordering
        tupleOrder (c1, x1) (c2, x2) 
        -- char compared by ord and a is less than b!
          | x1 == x2 && c1 <= c2 = GT 
          | x1 == x2 && c1 >= c2 = LT
          | x1 < x2 = LT 
          | x1 > x2 = GT

and then did sortBy.


Erlend

I’ll try that - Monoids have such an understated elegance. :)


> On 13 Dec 2016, at 16:01, Erlend Hamberg <erl...@hamberg.no> wrote:
> 
> There is a really nice solution that takes advantage of Ordering's Monoid 
> instance (see https://wiki.haskell.org/Monoid 
> <https://wiki.haskell.org/Monoid>).
> 
> The imports you need:
> 
> import Data.List (sortBy)
> import Data.Ord (Down(..), comparing)
> import Data.Monoid ((<>)) -- the “mappend” operator
> 
> You can then combine two calls to `comparing` 
> 
> sortBy (comparing  (Down . snd) <> comparing fst) xs
> 
> (`Down` is just a newtype that reverses the ordering, since you wanted the 
> first element in descending order and the second in ascending order.)
> 
> On Tue, 13 Dec 2016 at 16:30 Francesco Ariis <fa...@ariis.it 
> <mailto:fa...@ariis.it>> wrote:
> On Tue, Dec 13, 2016 at 02:36:39PM +0000, mike h wrote:
> > Hi,
> >
> > I’m trying to sort a list of tuples. A char and a count of that char (Char 
> > , Int)
> > e.g.
> >
> > [ ('r',2), ('c',2),('a', 2), ('b',3), ('f',2)]
> >
> > e.g. ‘r’ occurs twice etc.
> > The order should be based on the count first and then ties broken by the
> > natural ordering of char.
> 
> You should provide sortBy with an appropriate compare function, e.g.
> 
>     comp (a,b) (c,d) | a > c = GT
>                      | -- etc etc.
> 
> or go with the manky but working hack:
> 
> λ> :m Data.List
> λ> sortOn (\(a, b) -> b*(-100) + fromEnum a) [('r',2), ('c',2),('a', 2), 
> ('b',3), ('f',2)]
> [('b',3),('a',2),('c',2),('f',2),('r',2)]
> 
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org <mailto:Beginners@haskell.org>
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners 
> <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
> -- 
> Erlend Hamberg
> erl...@hamberg.no 
> <mailto:erl...@hamberg.no>_______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20161213/84cc3c53/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 102, Issue 4
*****************************************

Reply via email to