Created
July 16, 2015 00:00
-
-
Save jtobin/4f61f889a858bf4a5060 to your computer and use it in GitHub Desktop.
longest ordered word in a dictionary
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
module Ordered where | |
import Data.Function (on) | |
import Data.List (sort, maximumBy) | |
longestOrdered :: Ord a => [[a]] -> Maybe [a] | |
longestOrdered dict = safeMaximumBy (compare `on` length) | |
[word | word <- dict, sort word == word] | |
safeMaximumBy :: (a -> a -> Ordering) -> [a] -> Maybe a | |
safeMaximumBy _ [] = Nothing | |
safeMaximumBy p xs = Just (maximumBy p xs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment