## Concept:

# Type class Ord

## Headline

A Language:Haskell type class for ordering

## Description

The type class is concerned with types that admit comparison.

```
class Eq a => Ord a where
compare :: a -> a -> Ordering
(<) :: a -> a -> Bool
(<=) :: a -> a -> Bool
(>) :: a -> a -> Bool
(>=) :: a -> a -> Bool
max :: a -> a -> a
min :: a -> a -> a
```

These are essentially familiar comparison operators.

*compare* returns a value of an enum type to describing ordering.

```
data Ordering = LT | EQ | GT
```

*LT* should be read as "less than", *EQ* as "equal", and *GT* as "greater than".

It is sufficient to either define *compare* or "<=". All the other operators can be derived in a regular manner. Here is important to notice that every instance of *Ord* must also be an instance of *Eq*; see the type-class constraint above. Thus, *<* ad *<=* can be easily distinguished with the help of *Eq*.

## Illustration

Consider the following data type for natural numbers: the *Zero* constructor should be thought as representing "0"; the *Succ* constructor should be thought of as representing the successor function (i.e., addition of 1 or increment).

```
data Nat = Zero | Succ Nat
```

Equality and ordering could be reasonably defined for *Nat* as follows.

```
instance Eq Nat where
Zero == Zero = True
Zero == (Succ _) = False
(Succ _) == Zero = False
(Succ x) == (Succ y) = x == y
instance Ord Nat where
compare Zero Zero = EQ
compare Zero (Succ _) = LT
compare (Succ _) Zero = GT
compare (Succ x) (Succ y) = compare x y
```

Equality and ordering is exercised at the Haskell prompt as follows.

```
> Zero == Zero
True
> Zero == Succ Zero
False
> Succ Zero == Succ Zero
True
> Zero < Succ Zero
True
> Succ Zero < Succ Zero
False
```

## Backlinks

### There are no revisions for this page.

## User contributions

## User edits

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**

#### 10 most similar pages:

- Concept:Total order - 2.6136038
- Concept:Type class - 1.2662911
- Concept:Equality - 1.2179656
- Concept:Semantic equality - 1.048346
- Concept:Type-class constraint - 0.94852906
- Concept:Sorting problem - 0.8744637
- Concept:Binary search - 0.684657
- Concept:Sorting_problem - 0.65587014
- Technology:QuickCheck - 0.633469
- Concept:Linear search - 0.53921235

## Syntax for editing wiki

For you are available next options:will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from
**
@worker.
**