トップ «前の日記(2010-06-10) 最新 次の日記(2010-06-12)» 編集

日々の破片

著作一覧

2010-06-11

_ 10日の続き

で、Haskellで書いたらこうなった。
check :: String -> (Char, Int) -> ([Int], String)
check key (x, i) | elem x key = ([i], [x])
                 | otherwise = ([], [])
 
comp :: String -> ([Int], String) -> Bool
comp key v = length key == length (snd v)
 
pile :: ([Int], String) -> ([Int], String) -> ([Int], String)
pile (is, x) (is2, x2) | x == x2          = (is2, x2)
                       | elem (head x2) x = (is, x)
                       | otherwise        = (is ++ is2, x ++ x2)
                
pileAll :: String -> [([Int], String)] -> [([Int], String)]
pileAll _ [] = []
pileAll key (v:vs) = (foldl pile v vs) : (pileAll key vs)
 
findSections :: String -> String -> [([Int], String)]
findSections key str = filter (comp key) $ pileAll key 
                       [(a, s) | (a, s) <- map (check key) $ zip str [0..], not $ null a]
 
main :: IO ()
main = do
--  readFile "nkdata.txt" >>= putStrLn . show . (findSections "pma")
  putStrLn $ show (findSections "pma" "program-promnade")

約1時間30分かかった(実際に動くまで。推敲は別)。


2003|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|11|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

ジェズイットを見習え