Created
November 18, 2011 15:43
-
-
Save shangaslammi/1376795 to your computer and use it in GitHub Desktop.
A simple NFA in Haskell
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
-- See: http://swizec.com/blog/strangest-line-of-python-you-have-ever-seen/swizec/3012 | |
import Control.Monad | |
import Data.Maybe | |
import System.Environment | |
data NFA s i = NFA s [s] [((s,i),[s])] | |
runNFA (NFA i f t) = any (`elem` f) . foldM step i where | |
step s c = fromMaybe [] $ lookup (s,c) t | |
parseNFA s = NFA i fs t where | |
[_,i]:(_:fs):ts = map words $ lines s | |
t = map (\(s:[i]:_:s') -> ((s,i),s')) ts | |
main = do | |
nfa <- fmap parseNFA $ readFile "machine.txt" | |
getArgs >>= mapM_ (\i -> print (i, if runNFA nfa i then "YES" else "NO")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment