
On 03/03/14 05:33, Tyler Huffman wrote:
Hi all,
I'm having trouble finding resources explaining how Ord for two lists works.
I've created a public gist to explain what I mean.
https://gist.github.com/iduhetonas/9318958
Could anybody shed some light on what I'm seeing?
Regards, Tyler Huffman
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
It does element-wise comparisons and in case one of the lists is empty, the other one is considered greater (unless they are both empty). So: Prelude> max [1] [2] Here 2 is bigger than 1 so we get the result straight away Prelude> max [1,2] [1] Here the first element is equal so we compare the rest of lists: [2] and []. As the second list is empty, we return the first list. Prelude> max [2,1] [1] No problem at all here, 2 is greater than 1 so we just return the first list on the first comparison. Prelude> max [1,2] [1,2,4] This is exactly the same case as ‘max [1, 2] [1]’: we first compare the 1s, find they are equal and compare the rest of the list. Same with 2s. Then the base case is ‘max [] [4]’ so the second list is treated as greater. Prelude> max [1,2,4] [1,2] Same as above but with arguments switched. I believe that the law here is ‘max x y ≡ max y x’. Prelude> max [[1,2,3],[2,3,4]] [[1,2],[2,3],[2,4]] This is just like all our other examples except one more level of nesting. We first end up comparing the first elements of the argument (so we call out ‘max [1, 2, 3] [1, 2]’, and then check each element of these lists. 1 and 2 are equal and we reach the ‘max [3] []’ case: the first list was greater so our whole call returns ‘[[1, 2, 3], [2, 3, 4]]’. I hope that sheds some light. -- Mateusz K.