Skip to content

Instantly share code, notes, and snippets.

@shangaslammi
Created November 18, 2011 15:43
Show Gist options
  • Save shangaslammi/1376795 to your computer and use it in GitHub Desktop.
Save shangaslammi/1376795 to your computer and use it in GitHub Desktop.
A simple NFA in Haskell
-- 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