Skip to content

Instantly share code, notes, and snippets.

@rexcfnghk
Created September 25, 2021 10:06
Show Gist options
  • Save rexcfnghk/df5543ca19dea645952cf4bb286f79f2 to your computer and use it in GitHub Desktop.
Save rexcfnghk/df5543ca19dea645952cf4bb286f79f2 to your computer and use it in GitHub Desktop.
import Data.Foldable
secondLargest :: (Num a, Ord a, Foldable t) => t a -> a
secondLargest xs = snd . foldl' secondLargest' (0, 0) xs 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