Created
September 26, 2021 15:03
-
-
Save rexcfnghk/a3d43c381bd789d70022bd426f61cb75 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 :: (Ord a, Foldable t) => t a -> Maybe a | |
secondLargest = snd . foldl' secondLargest' (Nothing, Nothing) where | |
secondLargest' (Nothing, Nothing) x = (Just x, Just x) | |
secondLargest' (Just largest, Just second) x | |
| x > largest = (Just x, Just largest) | |
| x > second = (Just largest, Just x) | |
| otherwise = (Just largest, Just second) | |
secondLargest' tuple _ = tuple |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment