```
maxs :: (RealFloat a) => a -> a -> a
maxs a b
| a>b = a
| otherwise = b
maxs :: (Ord a) => a -> a -> a
maxs a b
| a>b = a
| otherwise = b
```

I'm trying to write a max function using Haskell. These two seem to work both. I wrote the first one and the latter one comes from the book. My question is, what's the difference between them? Can they replace each other?

The ord class is used for totally ordered datatypes. But in this case, order doesn't seem to matter.

# Best How To :

`RealFloat`

implies `RealFrac`

which implies `Real`

which in turn implies `Ord`

.

In other words, every type that is an instance of `RealFloat`

*must* also be an instance of `Ord`

. So *both* of these functions require `Ord`

(which is what defines the "`<`

" operator that you're trying to use). But the first one pointlessly *also* requires a bunch of stuff to do with floating-point numbers (e.g., the `cos`

function).

Order most certainly *does* matter; that's *the whole point* of a max function. It's to find the maximum value. Without an ordering, there *is* no maximum value!

By using `RealFrac`

, on the other hand, you are prevented from (for example) finding the maximum `String`

, since a `String`

clearly isn't any kind of floating-point number.