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(リストを使う)、練習問題 3.を解いてみる。
その他参考書籍
- すごいHaskellたのしく学ぼう!(オーム社) Miran Lipovača(著)、田中 英行、村主 崇行(翻訳)
- プログラミングHaskell (オーム社) Graham Hutton(著) 山本 和彦(翻訳)
練習問題 3.
コード(BBEdit, Emacs)
InteractWith.hs
{-# OPTIONS -Wall -Werror #-}
module Main where
import System.Environment (getArgs)
main :: IO ()
main = mainWith myFunction
where 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 = myFunction' (lines s)
myFunction' :: [String] -> String
myFunction' [] = []
myFunction' (l:ls) = myFunction'' l ++ '\n':myFunction' ls
myFunction'' :: String -> String
myFunction'' l = if ws /= [] then head ws else ""
where ws :: [String]
ws = words l
interactWith :: (String -> String) -> FilePath -> FilePath -> IO ()
interactWith function inputFile outputFile = do
input <- readFile inputFile
writeFile outputFile (function input)
入出力結果(Terminal, runghc)
$ runghc InteractWith.hs InteractWith.hs output.txt
$ cat output.txt
{-#
module
import
main
main
where
mainWith
args
case
[input,
_
myFunction
myFunction
myFunction'
myFunction'
myFunction'
myFunction''
myFunction''
where
ws
interactWith
interactWith
input
writeFile
$
0 コメント:
コメントを投稿