Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import Text.Regex
import Control.Monad.State
longestWord' :: [String] -> State (String, Int) ()
longestWord' [] = return ()
longestWord' (x:xs) = get >>= \x1@(word, idx) ->
put (x, idx+1) >>
longestWord' xs >>
get >>= \x2@(word', idx') ->
if length word < length word' then put x2 else put x1
longestWord (x:xs) = runState (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