Skip to content

Instantly share code, notes, and snippets.

@jutememo
Created February 13, 2011 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jutememo/824329 to your computer and use it in GitHub Desktop.
Save jutememo/824329 to your computer and use it in GitHub Desktop.
import Text.Regex
import Control.Monad.State
longestWord' :: [String] -> State (String, Int) ()
longestWord' [] = return ()
longestWord' (x:xs) = do x1@(word, idx) <- get
put (x, idx+1)
longestWord' xs
x2@(word', idx') <- get
if length word < length word' then put x2 else put x1
longestWord (x:xs) = execState (longestWord' xs) (x, 0)
main = print $ longestWord $ splitRegex (mkRegex " ") "The quick brown fox"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment