
Hello everyone, I would like to ask something that I found in the ebook "a Gentle Introduction to Haskell". http://haskell.org/tutorial/stdclasses.html#sect8.4 data Tree a = Leaf a | Branch (Tree a) (Tree a) instance (Ord a) => Ord (Tree a) where (Leaf _) <= (Branch _) = True (Leaf x) <= (Leaf y) = x <= y (Branch _) <= (Leaf _) = False (Branch l r) <= (Branch l' r') = l == l' && r <= r' || l <= l' The latter specifies a lexicographic order: Constructors are ordered by the order of their appearance the data declaration, and the arguments of a constructor are compared from left to right. Although I have tried to make sense what lexicographic order means I haven't figured out. Maybe an example with a simple application of this would be helpful. To be honest I can't understand what the symbol <= really means. Thanks -- View this message in context: http://www.nabble.com/lexicographic-order-tp16381434p16381434.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

Although I have tried to make sense what lexicographic order means I haven't figured out. Maybe an example with a simple application of this would be helpful. To be honest I can't understand what the symbol <= really means.
<= means "less than or equal to". Normally, lexicograpic order is the order in which words would appear in a lexicon. This can be generalized to other data types than strings by the kind of comparison done here, by specifying an ordering of the constructors (in this case Leaf < Branch). Cheers, /Niklas

Hello Simeon, Monday, March 31, 2008, 12:45:54 AM, you wrote:
The latter specifies a lexicographic order: Constructors are ordered by the order of their appearance the data declaration, and the arguments of a constructor are compared from left to right.
Although I have tried to make sense what lexicographic order means I haven't figured out. Maybe an example with a simple application of this would be helpful. To be honest I can't understand what the symbol <= really means.
i'm not sire that i understood your question (are you really never seen less-or-equal comparison? :), but i can say about lex. order: if you can compare chars and 'a' < 'b', then *lists* of chars compared in lexicographic order will be "aaa" < "aab" "aab" < "aba" "baa" < "abb" and so on - i.e. it finds *left-most* pair of non-equal chars and returns result based on it comparison the same principle used for comparison of these trees - any Leaf smaller than any Branch, if the same constructors are used then their parameters are compared, from left to right although the last alternative, (Branch l r) <= (Branch l' r') = l == l' && r <= r' || l <= l' seems suspicious to me. isn't it the same as (Branch l r) <= (Branch l' r') = l <= l' ? -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Hello Bulat, Monday, March 31, 2008, 1:16:35 AM, you wrote:
if you can compare chars and 'a' < 'b', then *lists* of chars compared in lexicographic order will be
"aaa" < "aab" "aab" < "aba" "baa" < "abb"
as it was mentioned by Niklas Broberg, the last sentence should be reversed: "abb" < "baa" sorry for +1 confusion :) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

2008/3/30, Bulat Ziganshin
although the last alternative, (Branch l r) <= (Branch l' r') = l == l' && r <= r' || l <= l' seems suspicious to me. isn't it the same as (Branch l r) <= (Branch l' r') = l <= l'
Yes, it should be : (Branch l r) <= (Branch l' r') = l < l' || l == l' && r <= r' Lexical order for a tuple (a,b) is : (a,b) <= (a',b') iff (a < a' or (a == a' and b <= b')) The same idea can be applied to list (where Nil is strictly less than anything else) or other datatypes. -- Jedaï

Chaddaï Fouché-2 wrote:
2008/3/30, Bulat Ziganshin
: although the last alternative, (Branch l r) <= (Branch l' r') = l == l' && r <= r' || l <= l' seems suspicious to me. isn't it the same as (Branch l r) <= (Branch l' r') = l <= l'
Yes, it should be : (Branch l r) <= (Branch l' r') = l < l' || l == l' && r <= r'
Lexical order for a tuple (a,b) is : (a,b) <= (a',b') iff (a < a' or (a == a' and b <= b')) The same idea can be applied to list (where Nil is strictly less than anything else) or other datatypes.
-- Jedaï _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Chaddaï Fouché-2 wrote:
2008/3/30, Bulat Ziganshin
: although the last alternative, (Branch l r) <= (Branch l' r') = l == l' && r <= r' || l <= l' seems suspicious to me. isn't it the same as (Branch l r) <= (Branch l' r') = l <= l'
Yes, it should be : (Branch l r) <= (Branch l' r') = l < l' || l == l' && r <= r'
Lexical order for a tuple (a,b) is : (a,b) <= (a',b') iff (a < a' or (a == a' and b <= b')) The same idea can be applied to list (where Nil is strictly less than anything else) or other datatypes.
-- Jedaï _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Quoted from: http://www.nabble.com/lexicographic-order-tp16381434p16388762.html Thanks for your help...I feel so embarrassed. With all of these symbolos in Haskell like -> (for types), => (in classes) etc I couldn't imagine that <= means less than or equal to!!!:clap:
Yes, it should be : (Branch l r) <= (Branch l' r') = l < l' || l == l' && r <= r'
Lexical order for a tuple (a,b) is : (a,b) <= (a',b') iff (a < a' or (a == a' and b <= b'))
I have tested it and I have found that it is the same with (Branch l r) <= (Branch l' r') = (l == l' && r <= r') || l < l' The problem with the previous is that the compiler during the parsing takes as right (Branch l r) <= (Branch l' r') = l == l' && (r <= r') || l < l') since the infix operator && needs two operands,i.e. one on the left and the other on the right Though I can't understand why both (Branch l r) <= (Branch l' r') = l < l' || l == l' && r <= r' (Branch l r) <= (Branch l' r') = l <= l' || l == l' && r <= r' give the same results and why I should take as right (a,b) <= (a',b') iff (a < a' or (a == a' and b <= b')) and not (a,b) <= (a',b') iff (a <= a' or (a == a' and b <= b')) The latter seems more logical, doesn't it? -- View this message in context: http://www.nabble.com/lexicographic-order-tp16381434p16392557.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

2008/3/31, Simeon Mattes
why I should take as right
(a,b) <= (a',b') iff (a < a' or (a == a' and b <= b'))
and not
(a,b) <= (a',b') iff (a <= a' or (a == a' and b <= b'))
The latter seems more logical, doesn't it?
No, it doesn't, since in the latter (1,2) <= (1,1) because 1 <= 1
Though I can't understand why both (Branch l r) <= (Branch l' r') = l < l' || l == l' && r <= r' (Branch l r) <= (Branch l' r') = l <= l' || l == l' && r <= r' give the same results
They don't, the second is a mistake, the first is the right one. -- Jedaï
participants (4)
-
Bulat Ziganshin
-
Chaddaï Fouché
-
Niklas Broberg
-
Simeon Mattes