[Haskell-beginners] Question about Ord for lists and multi-dimensional lists
tyler.huffman at tylerh.org
Mon Mar 3 05:47:13 UTC 2014
Awesome, that helps a lot.
Thanks a bunch, Mateusz!
On Sun, Mar 2, 2014 at 10:44 PM, Mateusz Kowalczyk
<fuuzetsu at fuuzetsu.co.uk>wrote:
> On 03/03/14 05:33, Tyler Huffman wrote:
> > Hi all,
> > I'm having trouble finding resources explaining how Ord for two lists
> > 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 at 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).
> Prelude> max  
> Here 2 is bigger than 1 so we get the result straight away
> Prelude> max [1,2] 
> Here the first element is equal so we compare the rest of lists:  and
> . As the second list is empty, we return the first list.
> Prelude> max [2,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] ’: 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  ’ so the second list is treated as
> 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  ’ 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.
> Beginners mailing list
> Beginners at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners