開発環境
- 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(著)長尾 高弘(翻訳))の5章(リスト)、5.13(練習問題)、1から13をHaskellで解いてみる。
その他参考書籍
- プログラミングHaskell (オーム社) Graham Hutton(著) 山本 和彦(翻訳)
- Real World Haskell―実戦で学ぶ関数型言語プログラミング (オライリージャパン) Bryan O'Sullivan John Goerzen Don Stewart(著) 山下 伸夫 伊東 勝利 株式会社タイムインターメディア(翻訳)
4.8(練習問題)、3.
コード(BBEdit)
Sample.hs
{-# OPTIONS -Wall -Werror #-}
import qualified Data.List as List
main :: IO ()
main = do
putStrLn "1."
putStrLn $ show alkalineEartnMetals
putStrLn "2."
putStrLn $ "ラジウムの原子番号" ++ (show $ alkalineEartnMetals !! 5) ++
" " ++ (show $ last alkalineEartnMetals)
putStrLn "3.リストの要素数はlength関数で取得"
putStrLn $ show $ length alkalineEartnMetals
putStrLn "4.リストの最大値を取得するmax関数"
putStrLn $ show $ List.maximum alkalineEartnMetals
putStrLn "5. putStrとputStrLnの違い"
putStr "a"
putStrLn "a"
putStrLn "6. リストに含まれる全ての値を1行に1つずつ表示"
putStrLn $ "リスト: " ++ show halfLives
mapM_ putStrLn $ map show halfLives
putStrLn "7. リストに含まれる全ての値を同じ行に表示"
mapM_ putStr $ map ((++" ") . show) halfLives
putStrLn ""
putStrLn "8. リストの要素の値の合計(sum)"
putStrLn $ "自作sum: " ++ (show $ mySum countryPopulations)
putStrLn $ "sum : " ++ (show $ sum countryPopulations)
putStrLn "9. 摂氏の気温のリスト"
putStrLn $ show temps
putStrLn "10. 数値のリストをソート"
putStrLn $ show sortedTemps
putStrLn "11."
mapM_ putStrLn $ map show [coolTemps, warmTemps]
putStrLn "12. 摂氏"
putStrLn $ show tempsInCelsius
putStrLn "13. 華氏に変換"
putStrLn $ show tempsInFahrenheit
alkalineEartnMetals :: [Int]
alkalineEartnMetals = [4, 12, 20, 38, 56, 88]
halfLives :: [Double]
halfLives = [87.74, 24110.0, 6537.0, 14.4, 376000.0]
countryPopulations :: [Int]
countryPopulations = [1295, 23, 7, 3, 47, 21]
mySum :: [Int] -> Int
mySum = foldr1 (+)
temps :: [Double]
temps = [25.2, 16.8, 31.4, 23.9, 28.0, 22.5, 19.6]
sortedTemps :: [Double]
sortedTemps = List.sort temps
coolTemps :: [Double]
coolTemps = takeWhile (\x -> if x < 20 then True else False) sortedTemps
warmTemps :: [Double]
warmTemps = drop (length coolTemps) sortedTemps
tempsInCelsius :: [Double]
tempsInCelsius = coolTemps ++ warmTemps
tempsInFahrenheit :: [Double]
tempsInFahrenheit = map (\x -> x * 9 / 5 + 32) tempsInCelsius
入出力結果(Terminal, runghc)
$ runghc Sample.hs 1. [4,12,20,38,56,88] 2. ラジウムの原子番号88 88 3.リストの要素数はlength関数で取得 6 4.リストの最大値を取得するData.Listモジュールのmaximum関数 88 5. putStrとputStrLnの違い aa 6. リストに含まれる全ての値を1行に1つずつ表示 リスト: [87.74,24110.0,6537.0,14.4,376000.0] 87.74 24110.0 6537.0 14.4 376000.0 7. リストに含まれる全ての値を同じ行に表示 87.74 24110.0 6537.0 14.4 376000.0 8. リストの要素の値の合計(sum) 自作sum: 1396 sum : 1396 9. 摂氏の気温のリスト [25.2,16.8,31.4,23.9,28.0,22.5,19.6] 10. 数値のリストをソート [16.8,19.6,22.5,23.9,25.2,28.0,31.4] 11. [16.8,19.6] [22.5,23.9,25.2,28.0,31.4] 12. 摂氏 [16.8,19.6,22.5,23.9,25.2,28.0,31.4] 13. 華氏に変換 [62.24,67.28,72.5,75.02,77.36,82.4,88.52] $
{-# OPTIONS -Wall -Werror #-}を記述してるから、細かく型を指定(:: Double)しないと警告がいっぱい出た。慣れるまでは{-# OPTIONS -Wall -Werror #-}の記述を消さずに細かく型を指定していくことに。
0 コメント:
コメントを投稿