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(リストを使う)の練習問題4.を解いてみる。
4.
コード(BBEdit)
Sample.hs
-- file: Sample.hs import System.Environment (getArgs) interactWith function inputFile outputFile = do input <- readFile inputFile writeFile outputFile (function input) swapRowAndCol :: String -> String swapRowAndCol a = unlines b where b = cols c cols [] = [] cols xs = (col xs):(cols (func xs)) col [] = [] col ((x:xs):[]) = x:[] col ((x:xs):ys) = x:(col ys) func [] = [] func ((x:xs):ys) | null xs = func ys | otherwise = xs:(func ys) c = filter notNull (lines a) notNull x | null x = False | otherwise = True 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 = swapRowAndCol
入出力結果(Terminal)
$ ghc --make Sample.hs [1 of 1] Compiling Main ( Sample.hs, Sample.o ) Linking Sample ... $ cat in.txt hello world $ ./Sample in.txt error: exactly two arguments needed $ ./Sample in.txt out.txt $ cat out.txt hw eo lr ll od $ ./Sample tmp.txt out.txt $ cat tmp.txt JavaScript HTML5 CSS Perl CPAN CGI Python Programmer Ruby on Rails Japan Haskell $ cat out.txt JPPRJ aeyua vrtbpH alhyaa S o ns cCnok rP ne iAP l pNrRl t oa Cgi HGrl TIas Mm Lm 5e r C S S $
0 コメント:
コメントを投稿