The comparison checks each pair of values in turn and it can stop when an element in one list is not equal to its counterpart in the other list, because now it can answer the question "is X > Y?". It's like an alphabetical comparison e.g. "BBC" > "ACD".
I don't know how Haskell implements this check, but for your lists [3,2,1] and [2,10,100] the comparison might be something like:
Is 3 = 2? No, so check if 3 > 2: ==> true and we're done.
If your lists were [3,2,1] and [3,10,100], then the comparison might be:
Is 3 = 3? Yes, so check next element instead:
Is 2 = 10? No, so check if 2 > 10: ==> false
The real implementation may do the = and > checks in a different order and it will also handle empty lists, but the basic principle is clear.
ex-Oracle bloke
Punit Jain
Ranch Hand
Joined: Aug 20, 2011
Posts: 902
posted
0
chris webster wrote:The comparison checks each pair of values in turn and it can stop when an element in one list is not equal to its counterpart in the other list, because now it can answer the question "is X > Y?". It's like an alphabetical comparison e.g. "BBC" > "ACD".
I don't know how Haskell implements this check, but for your lists [3,2,1] and [2,10,100] the comparison might be something like:
Is 3 = 2? No, so check if 3 > 2: ==> true and we're done.
If your lists were [3,2,1] and [3,10,100], then the comparison might be:
Is 3 = 3? Yes, so check next element instead:
Is 2 = 10? No, so check if 2 > 10: ==> false
The real implementation may do the = and > checks in a different order and it will also handle empty lists, but the basic principle is clear.
looks like in each (ie. <, >), it first checks for =.
but what is the use for that, however i want to check for >.
It doesn't really matter, because you still need to do the same checks in most cases. If element X is not > element Y, then you still need to know if X = Y, in which case you need to check the next pair of elements, or if X < Y, in which case you know that list X is not > list Y.
So let's try it your way with lists [3, 2, 1] and [2, 10, 100]:
Is [3, 2, 1] > [2, 10, 100]?
Is 3 > 2? Yes, so return true and stop. List [3, 2, 1] is > list [2, 10, 100].
But if you try it with lists [3, 2, 1] and [3, 10, 100]:
Is 3 > 3? No, so check: is 3 = 3?
Yes, 3 = 3, so now we have to check the next element in the lists to see which is greater.
Is 2 > 10? No, so check: is 2 = 10? No, so 2 must be < 10, which means we can return false and stop, because list [3, 2, 1] is not > list [3, 10, 100].
Haskell compares each pair of elements in the lists in turn, and stops as soon as it can answer the question you asked i.e. is list X > list Y? Or when it reaches the end of one of the lists.
It's really not that hard. Think about how you compare two strings (lists of characters) alphabetically - it's the same principle. In fact, Learn You A Haskell tells you this:
Learn You A Haskell wrote:When using <, <=, > and >= to compare lists, they are compared in lexicographical order. First the heads are compared. If they are equal then the second elements are compared, etc.
As was said it's due to how the list are compared.
If you want to run the > operator against each pair from the lists you can use zipWith or in the case of [Int] you can sum them and then compare them.