问题
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?
回答1:
This evaluates to True
as 3 is greater than 2. Having found a result, the comparison stops here. He's demonstrating 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 lexicographical order would be: xyz1.txt
, xyz10.txt
, xyz2.txt
, xyz20.txt
回答2:
"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".
回答3:
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
回答4:
Example:
What happens when walking through the following?
[1,2,9,2] > [1,2,10,1] -- False
[ 1, 2, 9, 2]
[ 1, 2,10, 1]
- compare 1 > 1, equal? yes, continue to next comparison
- compare 2 > 2, equal? yes, continue to next comparison
- compare 9 > 10, equal? no, 9 is actually less, stop and return False
Other examples
[1,2,9,2] < [1,2,10,1] -- True
[1,2,3,4] <= [1,2,3,5] -- True
[1,2,3,4] >= [1,2,3,4] -- True
[1,2,3,4] <= [1,2,3,4] -- True
回答5:
I think LearnYouAHaskell would benefit from writing the word Only
.
First the heads are compared. *Only* if they are equal then the second elements are compared, etc.
So because 3 is greater than 2, the decision is complete and there is no need to check indexes 1 and 2.
[3,2,1] > [2,10,100] == [3] > [2]
It can also be thought of a bit like a sortable date
2019-07-06 > 2011-08-09
If you do that in your head, you checked the years first, and don't need to check the months or days now to know the the first date is greater.
回答6:
Just understand this: comparing two lists doesn't mean comparing the values of all the elements in each list. Rather it means simply comparing the lexical order of each element in list1 with the corresponding element in list2, and returning the result as soon as it found that the two elements are ordered differently lexically.
来源:https://stackoverflow.com/questions/3651144/comparing-lists-in-haskell-or-more-specifically-what-is-lexicographical-order