Real World Haskell
実戦で学ぶ関数型言語プログラミング
(オライリージャパン)
Bryan O'Sullivan (著) John Goerzen (著)
Don Stewart (著)
山下 伸夫 (翻訳) 伊東 勝利 (翻訳)
株式会社タイムインターメディア (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs (Text Editor)
- Haskell (純粋関数型プログラミング言語)
- GHC (The Glasgow Haskell Compiler) (処理系)
- The Haskell Platform (インストール方法、モジュール等)
Real World Haskell―実戦で学ぶ関数型言語プログラミング(Bryan O'Sullivan (著)、 John Goerzen (著)、 Don Stewart (著)、山下 伸夫 (翻訳)、伊東 勝利 (翻訳)、株式会社タイムインターメディア (翻訳)、オライリージャパン)の4章(関数プログラミング)、4.5(リストを使う)、練習問題 4.を解いてみる。
その他参考書籍
- すごいHaskellたのしく学ぼう!(オーム社) Miran Lipovača(著)、田中 英行、村主 崇行(翻訳)
- プログラミングHaskell (オーム社) Graham Hutton(著) 山本 和彦(翻訳)
練習問題 4.
コード(BBEdit, Emacs)
InteractWith.hs
{-# OPTIONS -Wall -Werror #-} module Main where import System.Environment (getArgs) main :: IO () main = mainWith myFunction mainWith :: (String -> String) -> IO () mainWith function = do args <- getArgs case args of [input, output] -> interactWith function input output _ -> putStrLn "error: exactly two arguments needed" myFunction :: String -> String myFunction s = let ls = lines s n = length ls m = maxLength ls l = if n > m then n else m in myFunction1 (rectangle l ls) maxLength :: [String] -> Int maxLength xs = maxLength1 xs 0 maxLength1 :: [String] -> Int -> Int maxLength1 [] n = n maxLength1 (x:xs) n = let m = length x in if m > n then maxLength1 xs m else maxLength1 xs n interactWith :: (String -> String) -> FilePath -> FilePath -> IO () interactWith function inputFile outputFile = do input <- readFile inputFile writeFile outputFile (function input) myFunction1 :: [String] -> String myFunction1 xs = joinN (myFunction2 xs) rectangle :: Int -> [String] -> [String] rectangle _ [] = [] rectangle n (x:xs) = (addSpaces (n - length x) x):rectangle n xs addSpaces :: Int -> String -> String addSpaces n s = if n == 0 then s else addSpaces (n - 1) (s ++ " ") joinN :: [String] -> String joinN [] = [] joinN (x:xs) = x ++ '\n':joinN xs myFunction2 :: [String] -> [String] myFunction2 [] = [] myFunction2 (x:[]) = heads x myFunction2 (x:xs) = zipWith (++) (heads x) (myFunction2 xs) heads :: String -> [String] heads [] = [] heads (x:xs) = [[x]] ++ heads xs
入出力結果(Terminal, runghc)
$ runghc InteractWith.hs temp.txt output.txt $ cat temp.txt The Project Gutenberg EBook of Punch, or the London Charivari, Vol. 108, May 11th, 1895, by Various This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included with this eBook or online at www.gutenberg.org/license Title: Punch, or the London Charivari, Vol. 108, May 11th, 1895 Author: Various Editor: Sir Francis Burnand Release Date: January 26, 2014 [EBook #44760] Language: English *** START OF THIS PROJECT GUTENBERG EBOOK PUNCH, VOL. 108, MAY 11TH, 1895 *** Produced by Malcolm Farmer, Lesley Halamek and the Online Distributed Proofreading Team at http://www.pgdp.net $ cat output.txt TM Tarw T A E R L * PD ha hlei i u d e a * ri ey im-t t t i l n * os souh l h t e g dt P1 ss e o o a u S ur r1 etet : r r s a T ci ot B h : : e g A eb jh onii P e R du e, oots u V S D : T t c k n a i a be t1 rue c r r t E O yd 8 ienB h i e n F G9 ssdo , o F : g MP u5 teo u r l T ar t, frrk o s a J i H lo e oi r n a s I co nb rcto c n h S of by thr t i u lr e tie h s a P me rV ho o e r R a ga entn B y O Fd r sel L u J ai Ei u ri o r 2 E rn Bo swmn n n 6 C mg ou ehse d a , T e os a o n rT k otoa n d 2 G ,e fsft 0 U a o o C 1 T Lm f aetw h 4 E e nvhw a N sa P yeew r [ B lt u or . i E E e n n.Pg v B R yh c e ru a o G t h ot r o Ht , aYje i k E ap noen , B l: o yucb # O a/ r w te V 4 O m/ hm r o 4 K ew t eaGg l 7 kw h ryu. . 6 P w e e to 0 U a. cer 1 ] N np L aong 0 C dg o tpb/ 8 H d n yel , , tp d n ri h. o oigc M V en n t e a O e c,Ln y L Ot C o is . n h sgce 1 l a tie 1 1 i r vn t 0 n i aes h 8 e v n e , , a di r ti 1 M i w n 8 A , iac 9 Y twl 5 V hau 1 o yd 1 l e T . od H r , 1 0 1 8 8 , 9 5 * * * $
0 コメント:
コメントを投稿