Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing lists in Haskell, or more specifically what is lexicographical order?

Tags:

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?

like image 659
bplus Avatar asked Sep 06 '10 11:09

bplus


People also ask

What means lexicographic order?

In mathematics, the lexicographic or lexicographical order (also known as lexical order, or dictionary order) is a generalization of the alphabetical order of the dictionaries to sequences of ordered symbols or, more generally, of elements of a totally ordered set.

What is the use of lexicographic ordering?

Defining Lexicographical Order Thus, lexicographical order is a way for formalizing word order where the order of the underlying symbols is given. In programming, lexicographical order is popularly known as Dictionary order and is used to sort a string array, compare two strings, or sorting array elements.

What is lexicographically increasing order?

When applied to numbers, lexicographic order is increasing numerical order, i.e. increasing numerical order (numbers read left to right). For example, the permutations of {1,2,3} in lexicographic order are 123, 132, 213, 231, 312, and 321. When applied to subsets, two subsets are ordered by their smallest elements.


1 Answers

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

like image 80
Thorsten Dittmar Avatar answered Sep 18 '22 12:09

Thorsten Dittmar