Created
September 17, 2017 08:18
-
-
Save pnlybubbles/a0e3536ae7c31f644c273e60198bd704 to your computer and use it in GitHub Desktop.
haskellのStateモナドでAutomaton
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 Control.Monad.Trans.State | |
data Auto = Auto { | |
getNode :: Int, | |
getValue :: [Int] | |
} deriving Show | |
type AutoOp = State Auto () | |
f :: Char -> AutoOp | |
f c = do | |
Auto n v <- get | |
put $ Auto (next n c) (n:v) | |
return () | |
next n c = case (n, c) of | |
(0, 'a') -> 1 | |
(0, _) -> 2 | |
(1, 'b') -> 3 | |
(1, _) -> 4 | |
(2, 'c') -> 3 | |
(2, _) -> 4 | |
(3, _) -> 5 | |
(4, _) -> 5 | |
_ -> -1 | |
main = do | |
let s0 = Auto 0 [] | |
print $ (`runState` s0) $ foldr1 (>>) (f <$> "abc") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment