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.