Skip to content

Instantly share code, notes, and snippets.

@rexcfnghk
Last active September 22, 2021 03:47
Show Gist options
  • Save rexcfnghk/4998dc50e7d51b81a89559784df3c7d3 to your computer and use it in GitHub Desktop.
Save rexcfnghk/4998dc50e7d51b81a89559784df3c7d3 to your computer and use it in GitHub Desktop.
Find the second largest Num in a Foldable
import Data.Foldable
secondLargest :: (Num a, Ord a, Foldable t) => t a -> a
secondLargest = snd . foldl' secondLargest' (0, 0) where
secondLargest' (largest, second) x
| x > largest = (x, largest)
| x > second = (largest, x)
| otherwise = (largest, second)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment