開発環境
- OS X Mavericks - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs (Text Editor)
- Haskell (純粋関数型プログラミング言語)
- GHC (The Glasgow Haskell Compiler) (処理系)
- The Haskell Platform (インストール方法、モジュール等)
初めてのコンピュータサイエンス(Jennifer Campbell、Paul Gries、Jason Montojo、Greg Wilson(著)長尾 高弘(翻訳))の2章(Python入門)の2.10(練習問題)、9をHaskellで解いてみる。
その他参考書籍
- プログラミングHaskell (オーム社) Graham Hutton(著) 山本 和彦(翻訳)
- Real World Haskell―実戦で学ぶ関数型言語プログラミング (オライリージャパン) Bryan O'Sullivan John Goerzen Don Stewart(著) 山下 伸夫 伊東 勝利 株式会社タイムインターメディア(翻訳)
9.
コード(BBEdit)
Sample.hs
{-# OPTIONS -Wall -Werror #-}
main :: IO ()
main = mapM_ putStrLn $
map (\(x, y) -> "距離 " ++ show (x :: Int) ++ "km 燃費 " ++
show (y :: Int) ++ " mpg 必要なガソリンの量 " ++
(show $ litresNeeded (fromIntegral x)
(fromIntegral y)) ++
"リットル")
[(150, 30), (100, 30)]
litresNeeded :: Double -> Double -> Double
litresNeeded a = (*a) . (/100) . convertMileage
convertMileage :: Double -> Double
convertMileage = (100/) . convertMpgToKmpl
convertMpgToKmpl :: Double -> Double
convertMpgToKmpl = (/gallonToLitre 1) . mileToKm
gallonToLitre :: Double -> Double
gallonToLitre = (3.78541178*)
mileToKm :: Double -> Double
mileToKm = (1.609344*)
入出力結果(Terminal, runghc)
$ runghc Sample.hs 距離 150km 燃費 30 mpg 必要なガソリンの量 11.760729154239241リットル 距離 100km 燃費 30 mpg 必要なガソリンの量 7.84048610282616リットル $
0 コメント:
コメントを投稿