Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System.IO
- import System.Environment
- import Data.List
- import Data.Maybe
- 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}
- readRule (x:y:z) = Rule {from = x, to = y, terminal = not $ null z}
- readRule _ = error "Wrong rule syntax"
- applyRule rules str = do s <- str
- rule <- find ((`isInfixOf` s) . from) rules
- return $ replace (from rule) (to rule) s
- main = do args <- getArgs
- rules <- return (head args) >>= readFile >>= return . map (readRule . split ' ') . lines
- mapM putStrLn $ zipWith (\step str -> show step ++ ": " ++ fromJust str) [0..]
- (takeWhile isJust . iterate (applyRule rules) . Just $ args !! 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement