2013年2月14日木曜日

開発環境

Real World Haskell』(Bryan O'SullivanJohn GoerzenDon Stewart(著)、山下 伸夫伊東 勝利株式会社タイムインターメディア(翻訳)、オライリー・ジャパン、2009年、ISBN978-4-87311-423-3)の4章(関数プログラミング)の4.5(リストを使う)の練習問題3.を解いてみる。

3.

コード(BBEdit)

Sample.hs

-- file: Sample.hs

import System.Environment (getArgs)

interactWith function inputFile outputFile = do
    input <- readFile inputFile
    writeFile outputFile (function input)

myFunc :: String -> String
myFunc a = (unwords b) ++ "\n"
    where c = lines a
          headWords [] = []
          headWords (x:xs) | null x = headWords xs
                           | otherwise = (head (words x)):(headWords xs)
          b = headWords c

main = mainWith myFunction
    where mainWith function = do
            args <- getArgs
            case args of
                [input, output] -> interactWith function input output
                _ -> putStrLn "error: exactly two arguments needed"
          myFunction = myFunc

入出力結果(Terminal)

$ ghc --make Sample.hs
[1 of 1] Compiling Main             ( Sample.hs, Sample.o )
Linking Sample ...
$ cat tmp.txt
JavaScript HTML5 CSS
Perl CPAN CGI
Python Programmer

Ruby on Rails
Japan
$ ./Sample tmp.txt out_tmp.txt
$ cat tmp.txt
JavaScript HTML5 CSS
Perl CPAN CGI
Python Programmer

Ruby on Rails
Japan
$ cat out_tmp.txt
JavaScript Perl Python Ruby Japan
$

0 コメント:

コメントを投稿