Skip to content

Instantly share code, notes, and snippets.

@jutememo
Created February 12, 2011 14:33
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/823793 to your computer and use it in GitHub Desktop.
Save jutememo/823793 to your computer and use it in GitHub Desktop.
import Text.Regex
type State s a = s -> (a, s)
comb :: State s a -> (a -> State s b) -> State s b
comb m k = \s -> let (x1, s') = m s in k x1 s'
comb_ :: State s a -> State s b -> State s b
comb_ m n = m `comb` \_ -> n
ret :: a -> State s a
ret x = \s -> (x, s)
longestWord' :: [String] -> State Int String
longestWord' [x] = ret x
longestWord' (x:xs) = longestWord' xs `comb` \x' ->
if length x < length x'
then inc `comb_` ret x'
else ret x
inc = \idx -> ((),idx +1)
longestWord xs = longestWord' xs 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