あみだくじ
Posted feedbacks - Haskell
どうでしょう
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | s = ["A B C D E",
"| | |-| |",
"|-| | |-|",
"| |-| |-|",
"|-| |-| |",
"|-| | | |"]
repl (x:' ':xs) ('|':y:ys) =
let z:zs = repl xs ys in
case y of
'-' -> z:' ':x:zs
_ -> x:' ':z:zs
repl xs _ = xs
main = putStrLn $ (unlines s) ++ foldl1 repl s
|
初心者なりにがんばってみました。出来上がってからMinkeさんの回答を見てアゴが勢いよく机に落ちました… まさしくエレガント。
大変勉強になりました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | module Main
where
import System (getArgs)
import Data.List (intersperse)
swaps :: String -> String -> String
swaps (px1:px2:pxs) (pl1:pl2:pls)
| pl1 == '-' = [px2, px1] ++ swaps pxs pls
| otherwise = [px1] ++ swaps (px2:pxs) (pl2:pls)
swaps xs _ = xs
followLines:: String -> String -> String
followLines pls line = swaps pls (filter (/= '|') line)
solveAmida :: [String] -> String
solveAmida (x:xs) = intersperse ' ' $ foldl (followLines) (filter (/= ' ') x) xs
main :: IO()
main = do
args <- getArgs
body <- readFile $ head args
putStrLn body
putStrLn $ solveAmida $ lines body
|


greentea #4476() Rating4/6=0.67
[ reply ]