入出力の練習(その2)。
import System
main = do args <- getArgs
mapM_ uniqFile args
uniqFile fileName = do contents <- readFile fileName
putStr $ unlines $ uniq $ lines contents
uniq [] = []
uniq (c:cs) = uniq' c cs
where uniq' str [] = [str]
uniq' str (c:cs)
| str == c = uniq' str cs
| otherwise = [str] ++ uniq' c cs
結果。
>cat sample.txt Perl Perl Ruby Ruby Ruby Javascript VBA Haskell Haskell >uniq sample.txt Perl Ruby Javascript VBA Haskell