How does Haskell order strings?

I recently studied Haskell, and I noticed that the type String (or [Char] ) can be ordered. For example, this is valid:

 ghci> "foo" > "bar" True ghci> "?<>!" `compare` "[&*}" LT 

How does Haskell order String s, and when will this functionality be useful?

+4
source share
4 answers

How does Haskell arrange strings and when will this functionality be useful?

First, Char is an Ord instance defined by equality primitives on the base primitive Char type on the machine.

 instance Ord Char where (C# c1) > (C# c2) = c1 `gtChar#` c2 (C# c1) >= (C# c2) = c1 `geChar#` c2 (C# c1) <= (C# c2) = c1 `leChar#` c2 (C# c1) < (C# c2) = c1 `ltChar#` c2 

then String is defined as [Char] (a Char list), and lists generally have an order if their elements have an order:

 instance (Ord a) => Ord [a] where compare [] [] = EQ compare [] (_:_) = LT compare (_:_) [] = GT compare (x:xs) (y:ys) = case compare xy of EQ -> compare xs ys other -> other 

and what is he. Any list whose elements are in order will in turn be ordered.

Since Char is ordered by its basic representation in the form of a bit pattern, and lists are set by ordering lists by elements, you see the behavior of String.

when will this feature be useful?

Insert rows into data structures that are polymorphic but require a sequencing method. The most noteworthy are Set and Map .

+9
source

How Haskell arranges strings

Here are some definitions from the Haskell Prelude .

Strings are just lists of characters:

 type String = [Char] 

Characters are sorted by Unicode code:

 instance Ord Char where c <= c' = fromEnum c <= fromEnum c' 

And the lists are compared using lexicographic order (implicitly by the structure of the list and the definition of the automatically received Ord ):

 data [a] = [] | a : [a] deriving Ord -- not actually valid Haskell :) instance Ord a => Ord [a] 

and when will this functionality be useful?

You need an Ord instance to use things like Map or Set .

+7
source

2 lists are compared in lexicographic order (i.e., from left to right) if each element is an instance of the Ord class. Lines can be ordered since Char can be ordered.

Try the following:

 [1,2,3] < [2,3,4,5] 
+2
source

I would suggest that the lexicographic order , for any character encoding. (In other words, the "alphabetical" order with a, for ASCII or other single-byte encodings, is the 256-character alphabet.)

+1
source

Source: https://habr.com/ru/post/1313991/


All Articles