Skip to content

minimum and maximum

Next we have maximum and minimum, which respectively return the maximum and minimum element in a non-empty container whose elements are of a type a that is orderable, an instance of Ord:

maximum :: Ord a => t a -> a
minimum :: Ord a => t a -> a

I won't discuss their default implementations because they perform a bit of somewhat advanced acrobatics with functors.

Here are the results of applying these functions to containers other than lists, again using the same old tree we have been using throughout:

GHCi
>>> minimum Nothing
*** Exception: minimum: empty structure
>>> minimum (Just 1)
1
>>> maximum tree
5

As the first example shows, maximum and minimum are defined only if the container is non-empty. What should the minimum or maximum element of an empty container be? There's no way to define a maximum or minimum function that can work with empty containers.