Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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