2013年11月21日木曜日

開発環境

初めてのコンピュータサイエンス(Jennifer CampbellPaul GriesJason MontojoGreg Wilson(著)長尾 高弘(翻訳))の9章(集合と辞書)、9.5(練習問題)、2.をHaskellで解いてみる。

その他参考書籍

9.5(練習問題)、2.

コード(BBEdit)

Sample.hs

{-# OPTIONS -Wall -Werror #-}

main :: IO ()
main = do
    putStrLn $ "males: " ++ show males
    putStrLn $ "females: " ++ show females
    putStrLn $ "mating pairs: " ++ (show $ matingPairs males females)


data Gerbil = Id Int | Name String
    deriving (Show)

matingPairs :: [Gerbil] -> [Gerbil] -> [(Gerbil, Gerbil)]
matingPairs a b = [(x, y) | x <- a, y <- b]

males :: [Gerbil]
males = [Id 1, Name "a", Id 2, Name "b", Id 3]

females :: [Gerbil]
females = [Name "c", Id 4, Id 5, Name "d", Name "e"]

入出力結果(Terminal, runghc)

$ runghc Sample.hs
males: [Id 1,Name "a",Id 2,Name "b",Id 3]
females: [Name "c",Id 4,Id 5,Name "d",Name "e"]
mating pairs: [(Id 1,Name "c"),(Id 1,Id 4),(Id 1,Id 5),(Id 1,Name "d"),(Id 1,Name "e"),(Name "a",Name "c"),(Name "a",Id 4),(Name "a",Id 5),(Name "a",Name "d"),(Name "a",Name "e"),(Id 2,Name "c"),(Id 2,Id 4),(Id 2,Id 5),(Id 2,Name "d"),(Id 2,Name "e"),(Name "b",Name "c"),(Name "b",Id 4),(Name "b",Id 5),(Name "b",Name "d"),(Name "b",Name "e"),(Id 3,Name "c"),(Id 3,Id 4),(Id 3,Id 5),(Id 3,Name "d"),(Id 3,Name "e")]
$

{-# OPTIONS -Wall -Werror #-}を記述してるから、細かく型を指定(:: Double)しないと警告がいっぱい出た。慣れるまでは{-# OPTIONS -Wall -Werror #-}の記述を消さずに細かく型を指定していくことに。

0 コメント:

コメントを投稿