開発環境
- 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(著)長尾 高弘(翻訳))の15章(データベース)、15.11(練習問題)、1-c, d.をHaskellで解いてみる。
その他参考書籍
- プログラミングHaskell (オーム社) Graham Hutton(著) 山本 和彦(翻訳)
- Real World Haskell―実戦で学ぶ関数型言語プログラミング (オライリージャパン) Bryan O'Sullivan John Goerzen Don Stewart(著) 山下 伸夫 伊東 勝利 株式会社タイムインターメディア(翻訳)
15.11(練習問題)、1-c, d.
コード(BBEdit)
Sample.hs
{-# OPTIONS -Wall -Werror #-} module Main where import Database.HDBC import Database.HDBC.Sqlite3 main :: IO () main = do conn <- connectSqlite3 "census.db" _ <- run conn "DROP TABLE Density" [] _ <- run conn "CREATE TABLE Density(State TEXT, Population INTEGER, AREA REAL)" [] _ <- run conn "INSERT INTO Density VALUES(\"ニューファンドランド・ラブラドール州\", 512930, 370501.69)" [] _ <- run conn "INSERT INTO Density VALUES(\"プリンスエドワードアイランド州\", 135294, 5684.39)" [] _ <- run conn "INSERT INTO Density VALUES(\"ノバスコシア州\", 908007, 52917.43)" [] _ <- run conn "INSERT INTO Density VALUES(\"ニューブランズウィック州\", 729498, 71355.67)" [] _ <- run conn "INSERT INTO Density VALUES(\"ケベック州\", 7237479, 1357743.08)" [] _ <- run conn "INSERT INTO Density VALUES(\"オンタリオ州\", 11410046, 907655.59)" [] _ <- run conn "INSERT INTO Density VALUES(\"マニトバ州\", 1119583, 551937.87)" [] _ <- run conn "INSERT INTO Density VALUES(\"サスカチュワン州\", 978933, 586561.35)" [] _ <- run conn "INSERT INTO Density VALUES(\"アルバータ州\", 2974807, 639987.12)" [] _ <- run conn "INSERT INTO Density VALUES(\"ブリティッシュコロンビア州\", 3907738, 926492.48)" [] _ <- run conn "INSERT INTO Density VALUES(\"ユーコン準州\", 28674, 474706.97)" [] _ <- run conn "INSERT INTO Density VALUES(\"ノースウェスト準州\", 37360, 1141108.37)" [] _ <- run conn "INSERT INTO Density VALUES(\"ヌナブト準州\", 26745, 1925460.18)" [] r <- quickQuery' conn "SELECT * FROM Density" [] mapM_ print r disconnect conn
入出力結果(Terminal, runghc)
$ runghc Sample.hs [SqlByteString "\227\131\139\227\131\165\227\131\188\227\131\149\227\130\161\227\131\179\227\131\137\227\131\169\227\131\179\227\131\137\227\131\187\227\131\169\227\131\150\227\131\169\227\131\137\227\131\188\227\131\171\229\183\158",SqlByteString "512930",SqlByteString "370501.69"] [SqlByteString "\227\131\151\227\131\170\227\131\179\227\130\185\227\130\168\227\131\137\227\131\175\227\131\188\227\131\137\227\130\162\227\130\164\227\131\169\227\131\179\227\131\137\229\183\158",SqlByteString "135294",SqlByteString "5684.39"] [SqlByteString "\227\131\142\227\131\144\227\130\185\227\130\179\227\130\183\227\130\162\229\183\158",SqlByteString "908007",SqlByteString "52917.43"] [SqlByteString "\227\131\139\227\131\165\227\131\188\227\131\150\227\131\169\227\131\179\227\130\186\227\130\166\227\130\163\227\131\131\227\130\175\229\183\158",SqlByteString "729498",SqlByteString "71355.67"] [SqlByteString "\227\130\177\227\131\153\227\131\131\227\130\175\229\183\158",SqlByteString "7237479",SqlByteString "1357743.08"] [SqlByteString "\227\130\170\227\131\179\227\130\191\227\131\170\227\130\170\229\183\158",SqlByteString "11410046",SqlByteString "907655.59"] [SqlByteString "\227\131\158\227\131\139\227\131\136\227\131\144\229\183\158",SqlByteString "1119583",SqlByteString "551937.87"] [SqlByteString "\227\130\181\227\130\185\227\130\171\227\131\129\227\131\165\227\131\175\227\131\179\229\183\158",SqlByteString "978933",SqlByteString "586561.35"] [SqlByteString "\227\130\162\227\131\171\227\131\144\227\131\188\227\130\191\229\183\158",SqlByteString "2974807",SqlByteString "639987.12"] [SqlByteString "\227\131\150\227\131\170\227\131\134\227\130\163\227\131\131\227\130\183\227\131\165\227\130\179\227\131\173\227\131\179\227\131\147\227\130\162\229\183\158",SqlByteString "3907738",SqlByteString "926492.48"] [SqlByteString "\227\131\166\227\131\188\227\130\179\227\131\179\230\186\150\229\183\158",SqlByteString "28674",SqlByteString "474706.97"] [SqlByteString "\227\131\142\227\131\188\227\130\185\227\130\166\227\130\167\227\130\185\227\131\136\230\186\150\229\183\158",SqlByteString "37360",SqlByteString "1141108.37"] [SqlByteString "\227\131\140\227\131\138\227\131\150\227\131\136\230\186\150\229\183\158",SqlByteString "26745",SqlByteString "1925460.18"] $
慣れるまでは{-# OPTIONS -Wall -Werror #-}の記述を消さずに細かく型を指定していくことに。
0 コメント:
コメントを投稿