Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System.IO
- import System.Environment
- import Data.List
- replace :: String -> String -> String -> String
- replace _ _ [] = []
- replace src dst s@(x:xs) = case stripPrefix src s of
- Just a -> dst ++ replace src dst a
- Nothing -> (x:replace src dst xs)
- split _ [] = [[]]
- split c xs = case span (/= c) xs of
- (f, []) -> [f]
- (f, r) -> (f:split c (tail r))
- data Rule = Rule {from, to :: String, terminal :: Bool}
- deriving Show
- readRule (x:y:z) = Rule {from = x, to = y, terminal = not $ null z}
- readRule _ = error "Wrong rule syntax"
- applyRule rule = replace (from rule) (to rule)
- main = do args <- getArgs
- rules <- return (head args) >>= readFile >>= return . map (readRule . split ' ') . lines
- let startStr = args !! 1
- putStrLn $ "0: " ++ startStr
- run 1 rules (args !! 1)
- where run step rules str = putStr (show step ++ ": ") >>
- case find ((`isInfixOf` str) . from) rules of
- Nothing -> putStrLn "No matching rule, stop."
- Just rule -> putStrLn newStr >> if terminal rule
- then putStrLn "Terminal rule, stop."
- else run (step + 1) rules newStr
- where newStr = applyRule rule str
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement