Graph

AA折れ線グラフ via shiroさんの所

Haskellのお勉強。

module Main where
import Data.List

plist :: (Int, Int) -> [(Int, Char)] -> String -> [(Int, Char)]
plist (px,py) lst [] = reverse lst
plist (px,py) lst (c:cs) = case c of
                           'R'  -> plist (px+1,py+1) ((py+0, '/'):lst) cs
                           'F'  -> plist (px+1,py-1) ((py-1,'\\'):lst) cs
                           'C'  -> plist (px+1,py+0) ((py+0, '_'):lst) cs
                           _    -> error "incorrect input"

plot :: [(Int, Char)] -> String
plot lst@(x:xs) = unlines [map (toChar2 row) lst | row <- [maxy .. miny]]
                  where (lsty, lstch) = unzip lst
                        maxy = maximum lsty
                        miny = minimum lsty
                        toChar2 :: Int -> (Int, Char) -> Char
                        toChar2 y1 (y2,ch) = if (y1 == y2)
                                             then ch
                                             else ' '

main :: IO ()
main = getLine >>= putStr . plot . plist (-1, 0) []

まぁぼちぼちかなぁ...plistもplotももっとエレガントに書けそう。

(追記)

向井さんのとか花谷さんのとか凄く参考になる。俺のんしょぼいな。