Skip to content

Instantly share code, notes, and snippets.

@pnlybubbles
Created September 17, 2017 08:18
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 pnlybubbles/a0e3536ae7c31f644c273e60198bd704 to your computer and use it in GitHub Desktop.
Save pnlybubbles/a0e3536ae7c31f644c273e60198bd704 to your computer and use it in GitHub Desktop.
haskellのStateモナドでAutomaton
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