Advertisement
Yurry

Очищенные алгорифмы

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