RosettaCodeData/Task/Ordered-words/Haskell/ordered-words-1.hs

20 lines
590 B
Haskell
Raw Permalink Normal View History

2013-04-10 12:38:42 -07:00
-- Words are read from the standard input. We keep in memory only the current
-- set of longest, ordered words.
--
-- Limitation: the locale's collation order is not take into consideration.
isOrdered wws@(_:ws) = and $ zipWith (<=) wws ws
2013-06-05 21:47:54 +00:00
longestOrderedWords = reverse . snd . foldl f (0,[]) . filter isOrdered
where f (max, acc) w =
let len = length w in
case compare len max of
LT -> (max, acc)
EQ -> (max, w:acc)
GT -> (len, [w])
2013-04-10 12:38:42 -07:00
main = do
str <- getContents
let ws = longestOrderedWords $ words str
mapM_ putStrLn ws