tags:

views:

123

answers:

3

Hi, I'm just beginning this nice hashkell beginners tutorial:

http://learnyouahaskell.com

on this page on lists he explains that lists are compared in compared in lexicographical order, he gives this example:

ghci> [3,2,1] > [2,10,100]
True

From some googling it seems to me that lexicographical ordering means in alphabetical or sequential number ordering(?), but I still can't make sense of this evaluating to True.

I'm missing something obvious here, can anybody help?

+7  A: 

"Lexicographic order" means similar to the way words are ordered in a dictionary: if the first element of each list is the same, compare the second elements; if the second elements are the same, compare the thirds; etc. If one list runs out of elements before the other, the shorter list is "less".

Yitz
Thanks for responding, I found Thorsten Dittmar a little bit easier to understand hence I ticked him.
bplus
+3  A: 

This evaluates to true as 3 is greater than 2. Having found a result, the comparison stops here. He's demomstrating that 2 and 10 are not compared. The result of the array comparison is true. If the first array started with 2 as well, the comparison would result in false.

A nice example for when lexicographical ordering does not result in what the user would expect is file names in windows. If you have files named xyz1.txt, xyz2.txt, xyz10.txt and xyz20.txt, the lexicogaphical order would be: xyz1.txt, xyz10.txt, xyz2.txt, xyz20.txt

Thorsten Dittmar
Thanks for the clear answer!
bplus
+4  A: 

In addition to the other answers: actual definition of instance Ord for lists [in GHC] pretty much says it all:

instance (Ord a) => Ord [a] where
    compare []     []     = EQ
    compare []     (_:_)  = LT
    compare (_:_)  []     = GT
    compare (x:xs) (y:ys) = case compare x y of
                                EQ    -> compare xs ys
                                other -> other
rkhayrov