Last active
September 22, 2021 03:47
-
-
Save rexcfnghk/4998dc50e7d51b81a89559784df3c7d3 to your computer and use it in GitHub Desktop.
Find the second largest Num in a Foldable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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