Real World Haskell
実戦で学ぶ関数型言語プログラミング
Bryan O'Sullivan, John Goerzen, Don Stewart(著)
山下 伸夫, 伊東 勝利
株式会社タイムインターメディア(翻訳)
開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- プログラミング言語: Haskell (純粋関数型)
Real World Haskell』(Bryan O'Sullivan、John Goerzen、Don 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 コメント:
コメントを投稿