Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
longest ordered word in a dictionary
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
You can’t perform that action at this time.