re: Find longest word in a given string VIEW POST

VIEW FULL DISCUSSION
 

Recently started digging my way through GHC libraries

import Data.List

cmpLength :: String -> String -> Ordering
cmpLength l r = compare (length r) (length l)

findLongest :: String -> String
findLongest s = head . sortBy (cmpLength) . words $ s

main :: IO ()
main = do
  putStrLn (findLongest "Iam a verrrrry loooongggg sentence")
import Data.List

longerString :: String -> String -> String
longerString l r = if length l > length r then l else r

findLongest :: String -> String
findLongest s = foldr (\longest w -> longerString longest w) "" (words s)

main :: IO ()
main = do
  putStrLn (findLongest "Iam a verrrrry loooongggg sentence")
code of conduct - report abuse