
I sent this only to Patrick by mistake. Sorry for the duplicate, Patrick. -- Patrick,
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?
"Ord a" isn't really the type of a pair... it's a type *constraint* on the argument to the sort function, which is otherwise any type "a". So it means List.sort can be applied to any type "a" which satisfies "Ord a". But what does that mean? It means that there is an instance for the typeclass Ord for whatever type is given to Listsort. In the case of pairs, it's exactly as you guessed... the leftmost parts are compared first. This is so because somewhere there's an "instance Ord (a,b)" that defines how to compare pairs, and it does it that way (left-to-right). (And this generalizes to tuples bigger than pairs, up to some compiler-specific limit.) Does this make sense? John

Hi,
Patrick,
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?
"Ord a" isn't really the type of a pair... it's a type *constraint* on the argument to the sort function, which is otherwise any type "a".
So it means List.sort can be applied to any type "a" which satisfies "Ord a". But what does that mean? It means that there is an instance for the typeclass Ord for whatever type is given to Listsort.
In the case of pairs, it's exactly as you guessed... the leftmost parts are compared first. This is so because somewhere there's an "instance Ord (a,b)" that defines how to compare pairs, and it does it that way (left-to-right). (And this generalizes to tuples bigger than pairs, up to some compiler-specific limit.)
Does this make sense?
Yes, perfectly. I guess you just have to know about it... :)
John
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

On Sat, Jul 11, 2009 at 5:57 PM, Patrick
LeBoutillier
Does this make sense?
Yes, perfectly. I guess you just have to know about it... :)
Well, it's the lexicographic order, the most "natural", in any case the most used order for tuples, so it would be strange to have other expectations by default Of course, the mechanism by which this work is interesting by itself. -- Jedaï

On Sat, Jul 11, 2009 at 1:13 PM, Chaddaï Fouché
On Sat, Jul 11, 2009 at 5:57 PM, Patrick LeBoutillier
wrote: Does this make sense?
Yes, perfectly. I guess you just have to know about it... :)
Well, it's the lexicographic order, the most "natural", in any case the most used order for tuples, so it would be strange to have other expectations by default
Of course, the mechanism by which this work is interesting by itself.
Really interesting in fact. It shows how generic these concepts really are. But it wasn't obvious to me that a pair could be an instance of Ord. This kind of gives you a clue: Prelude> :info () data () = () -- Defined in GHC.Unit instance Bounded () -- Defined in GHC.Enum instance Enum () -- Defined in GHC.Enum instance Eq () -- Defined in Data.Tuple instance Ord () -- Defined in Data.Tuple instance Read () -- Defined in GHC.Read instance Show () -- Defined in GHC.Show but is there a way to do the equivalent of: Prelude> :info (a,b) or Prelude> :info (Int,String) ? Patrick
-- Jedaï
-- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

Try Prelude> :info (,) data (,) a b = (,) a b -- Defined in GHC.Tuple instance (Bounded a, Bounded b) => Bounded (a, b) -- Defined in GHC.Enum instance (Eq a, Eq b) => Eq (a, b) -- Defined in Data.Tuple instance (Ord a, Ord b) => Ord (a, b) -- Defined in Data.Tuple instance (Read a, Read b) => Read (a, b) -- Defined in GHC.Read instance (Show a, Show b) => Show (a, b) -- Defined in GHC.Show On Sat, Jul 11, 2009 at 12:25 PM, Patrick LeBoutillier < patrick.leboutillier@gmail.com> wrote:
On Sat, Jul 11, 2009 at 1:13 PM, Chaddaï Fouché
wrote: On Sat, Jul 11, 2009 at 5:57 PM, Patrick LeBoutillier
wrote: Does this make sense?
Yes, perfectly. I guess you just have to know about it... :)
Well, it's the lexicographic order, the most "natural", in any case the most used order for tuples, so it would be strange to have other expectations by default
Of course, the mechanism by which this work is interesting by itself.
Really interesting in fact. It shows how generic these concepts really are. But it wasn't obvious to me that a pair could be an instance of Ord.
This kind of gives you a clue:
Prelude> :info () data () = () -- Defined in GHC.Unit instance Bounded () -- Defined in GHC.Enum instance Enum () -- Defined in GHC.Enum instance Eq () -- Defined in Data.Tuple instance Ord () -- Defined in Data.Tuple instance Read () -- Defined in GHC.Read instance Show () -- Defined in GHC.Show
but is there a way to do the equivalent of:
Prelude> :info (a,b) or Prelude> :info (Int,String)
?
Patrick
-- Jedaï
-- ===================== Patrick LeBoutillier Rosemère, Québec, Canada _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (4)
-
Chaddaï Fouché
-
Geoffrey Marchant
-
John Dorsey
-
Patrick LeBoutillier