Created
April 16, 2014 14:20
-
-
Save 314maro/10883478 to your computer and use it in GitHub Desktop.
マルコフアルゴリズム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Control.Applicative | |
| import Data.List (stripPrefix, intercalate) | |
| sub :: Eq a => [a] -> [a] -> [a] -> Maybe [a] | |
| sub from to s = (to ++) <$> stripPrefix from s <|> rec s | |
| where | |
| rec [] = Nothing | |
| rec (x:xs) = (x :) <$> sub from to xs | |
| step :: [(String,String)] -> String -> Maybe String | |
| step [] _ = Nothing | |
| step ((from,to):rs) s = sub from to s <|> step rs s | |
| run :: [(String,String)] -> String -> [String] | |
| run rs s = s : maybe [] (run rs) (step rs s) | |
| bin :: [(String,String)] | |
| bin = [ ("|0", "0||") | |
| , ("1", "0|") | |
| , ("0", "") | |
| ] | |
| main = do | |
| let s = "101" | |
| putStrLn $ intercalate " -> " $ run bin s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment