[Haskell-beginners] List.sort

Thomas Davie tom.davie at gmail.com
Sat Jul 11 09:13:27 EDT 2009


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
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners



More information about the Beginners mailing list