{- || -- ver.2
前回より、コメント削除関数をすっきりさせてみた
import System.IO import Data.List main = do handle <- openFile "haskell.hs" ReadMode; hGetContents handle >>= (\x -> putStrLn $ comment x) >> hClose handle comment [] = [] comment xs = case (take 2 xs) of "--" -> comment $ '\n':(unlines.tail $ lines xs) "{-" -> comment (drop 2.head$filter ((=="-}").take 2)$tails xs) otherwise -> head xs:(comment $ tail xs)
記事の都合上、main関数を3行にしていますが、実質合計11行
あんまり横にずらずら書くのは良くないですが。
行数1/2が目標だったので。
前回は確か20行だったような。
ただ、文字列内に
- -
{-
が出てきた時の処理はしていないです。
パターンマッチがあるから、どうしても、これ以上の縮小は難しいような