List.sort

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

List.sort

Patrick LeBoutillier
Hi,

I see the type of the List.sort function is:

  Ord a => [a] -> [a]

I don't understand how this type can allow it to sort pairs:

Prelude> :m +List
Prelude List> sort [(1, "1"), (3, "3"), (2, "2")]
[(1,"1"),(2,"2"),(3,"3")]

How can a pair by of type "Ord a"? Is it a rule that the first element
is automatically used?


Patrick

--
=====================
Patrick LeBoutillier
Rosem?re, Qu?bec, Canada
Reply | Threaded
Open this post in threaded view
|

List.sort

Thomas Davie
instance (Ord a, Ord b) => Ord (a,b) where
   compare (x,y) (z,w) =
     case compare x z of
       EQ -> compare y w
       c  -> c

Not certain that's the exact code that's used, but it's close enough.

Bob

On 11 Jul 2009, at 15:06, Patrick LeBoutillier wrote:

> Hi,
>
> I see the type of the List.sort function is:
>
>  Ord a => [a] -> [a]
>
> I don't understand how this type can allow it to sort pairs:
>
> Prelude> :m +List
> Prelude List> sort [(1, "1"), (3, "3"), (2, "2")]
> [(1,"1"),(2,"2"),(3,"3")]
>
> How can a pair by of type "Ord a"? Is it a rule that the first element
> is automatically used?
>
>
> Patrick
>
> --
> =====================
> Patrick LeBoutillier
> Rosem?re, Qu?bec, Canada
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners