Created
February 12, 2011 14:33
-
-
Save jutememo/823793 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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