View difference between Paste ID: GAaZnDch and pCPZRmgX
SHOW: | | - or go back to the newest paste.
1
import System.IO
2
import System.Environment
3
import Data.List
4
import Data.Maybe
5-
replace :: String -> String -> String -> String
5+
6
replace _ _ [] = []
7
replace src dst s@(x:xs) = case stripPrefix src s of
8
                                Just a -> dst ++ replace src dst a
9
                                Nothing -> (x:replace src dst xs)
10
                                
11
split _ [] = [[]]
12
split c xs = case span (/= c) xs of
13
                (f, []) -> [f]
14
                (f, r)  -> (f:split c (tail r))
15
16
data Rule = Rule {from, to :: String, terminal :: Bool}
17-
    deriving Show
17+
18
readRule (x:y:z) = Rule {from = x, to = y, terminal = not $ null z}
19
readRule _ = error "Wrong rule syntax"
20
21-
applyRule rule = replace (from rule) (to rule)
21+
applyRule rules str = do s <- str
22
                         rule <- find ((`isInfixOf` s) . from) rules
23
                         return $ replace (from rule) (to rule) s
24
25-
          let startStr = args !! 1
25+
26-
          putStrLn $ "0: " ++ startStr
26+
27-
          run 1 rules (args !! 1)
27+
          mapM putStrLn $ zipWith (\step str -> show step ++ ": " ++ fromJust str) [0..]
28-
       where run step rules str = putStr (show step ++ ": ") >>
28+
                                  (takeWhile isJust . iterate (applyRule rules) . Just $ args !! 1)