09/07/08 20:42:25
>>403
IO ばかりで正直あまり Haskell では書く気がしないな…
import System.IO
import Control.Exception (bracket)
import qualified Data.ByteString as B
t403_1 :: String -> IO ()
t403_1 fname = do
b <- bracket (openFile fname ReadMode) hClose parse
bracket (openFile fname WriteMode) hClose (flip B.hPut b)
where
parse :: Handle -> IO B.ByteString
parse h = hGetLine h >>= \s -> return $ B.pack $ read ("["++s++"]")
t403_2 :: String -> String -> String -> IO ()
t403_2 fname1 fname2 fname3 = do
b1 <- bracket (openFile fname1 ReadMode) hClose B.hGetContents
b2 <- bracket (openFile fname2 ReadMode) hClose B.hGetContents
bracket (openFile fname3 WriteMode) hClose (flip B.hPut (B.sort (B.concat [b1,b2])))
main = t403_1 "input1" >> t403_1 "input2" >> t403_2 "input1" "input2" "output"