or1ko's diary

日々を書きます

11

11. タブをスペースに置換
タブ1文字につきスペース1文字に置換せよ.確認にはsedコマンド,trコマンド,もしくはexpandコマンドを用いよ.

>sed "s/\t/ /g" hightemp.txt
>tr "\t" " " < hightemp.txt

expandはなかったためやめる。

11.hs

import System.IO.UTF8 as I8
import System.Environment
import Prelude as P

main = do
  filename <- getArgs >>= return . head
  body <- I8.readFile filename
  P.putStrLn $ unlines $ map (map f) $ lines body
    where
      f '\t' = ' '
      f x = x

読み込みはutf8-stringで、出力がPreludeという組み合わせで
うまくいくのがよくわからないが、ひとまずこれで。