Highest number of Smallest numbers Haskell

Viewed 60

So i have a list of lists, and i have to get out the minimum of every list and get the maximum of the minimums

EXAMPLE:

maximumOfMinimums [[1], [3, 5, 4], [8, 2]] == 3

This is how i tried:

maxMin :: [[a]] -> a
maxMin (x:xs) = minimum(map maximum (x:xs))
1 Answers

You did it the other way around. Here you are calculating the minimum of the maximums of every list. Furthermore there is a problem with the signature: you need to add a type constraint that a is an instance of the Ord typeclass:

maxMin :: Ord a => [[a]] -> a
maxMin xs = maximum (map minimum xs)

or as a point-free expression:

maxMin :: Ord a => [[a]] -> a
maxMin = maximum . map minimum

Note that this will not work if the list is empty, or one of the sublists is empty.

Related