Successfully reported this slideshow.

すごい Haskell 読書会 in 大阪 #5

1

Share

Loading in …3
×
1 of 20
1 of 20

More Related Content

Related Audiobooks

Free with a 14 day trial from Scribd

See all

すごい Haskell 読書会 in 大阪 #5

  1. 1. すごい Haskell 読書会 in 大阪 #5 岡本和樹 @kakkun61
  2. 2. 7.1 新しいデータ型を定義する • data Bool = True | False • data 型 = 値コンストラクター
  3. 3. 7.2 形づくる • 円 – x座標 – y座標 – 半径 • 四角形 – 左上x座標 – 左上y座標 – 右下x座標 – 右下y座標
  4. 4. 7.2 形づくる • data Shape = Circle Float Float Float | Rectangle Float Float Float Float • Circle :: Float -> Float -> Float -> Shape • Rectangle :: Float -> Float -> Float -> Float -> Shape
  5. 5. 7.2 形づくる • area :: Shape -> Float • area (Circle _ _ r) = pi * r ^ 2 • area (Rectangle x1 y1 x2 y2) = (abs $ x1 - x2) * (abs $ y1 - y2)
  6. 6. 7.2 形づくる • data Point = Point Float Float deriving (Show) • data Shape = Circle Point Float | Rectangle Point Point deriving (Show) • area :: Shape -> Float • area (Circle _ r) = pi * r ^ 2 • area (Rectangle (Point x1 y1) (Point x2 y2)) = …
  7. 7. 7.3 レコード構文 • data Car = Car String String Int deriving (Show) • Car "Ford" "Mustang" 1967
  8. 8. 7.3 レコード構文 • data Car = Car String String Int deriving (Show) • company :: Car -> String • company (Car company _ _) = company • model :: Car -> String • model (Car _ model _) = model • year :: Car -> Int • year (Car _ _ year) = year
  9. 9. 7.3 レコード構文 • data Car = Car { company :: String , model :: String , year :: Int } deriving (Show) • Car {year = 1990, model = "Mark II", company = "Toyota"} • Car "Ford" "Mustang" 1967
  10. 10. 7.4 型引数 • data Maybe a = Nothing | Just a • Just “Hay” :: Maybe String • [1..10] :: [Int] • [“learn”, “Haskell”] :: [String]
  11. 11. 7.4 型引数 • Map k v • data (Ord k) => Map k v ... • データ宣言には決して型クラス制約をつ けない • Map.toList :: Map k v -> [(k, v)]
  12. 12. 7.4 型引数 • data Vector a = Vector a a a deriving (Show) • vplus :: (Num t) => Vector t -> Vector t -> Vector t • (Vector i j k) `vplus` (Vector l m n) = Vector (i+l) (j+m) (k+n) • vectMult :: (Num t) => Vector t -> t -> Vector t • scalarMult :: (Num t) => Vector t -> Vector t -> t
  13. 13. 7.5 インスタンスの自動導出 • data Person = Person { firstName :: String , lastName :: String , age :: Int } deriving (Eq)
  14. 14. 7.5 インスタンスの自動導出 • deriving (Eq, Show, Read) • show :: Person -> String • read :: String -> Person
  15. 15. 7.5 インスタンスの自動導出 • ghci> read "Person {firstName =¥"Michael¥", lastName =¥"Diamond¥", age = 43}" :: Person • Person {firstName = "Michael", lastName = "Diamond", age = 43}
  16. 16. 7.5 インスタンスの自動導出 • data Bool = False | True deriving (Ord) • ghci> True `compare` False • GT • ghci> False < True • True
  17. 17. 7.5 インスタンスの自動導出 • data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving ( Eq, Ord, Show, Read , Bounded, Enum)
  18. 18. 7.5 インスタンスの自動導出 • Bounded クラス • ghci> minBound :: Day • Monday • ghci> maxBound :: Day • Sunday
  19. 19. 7.5 インスタンスの自動導出 • Enum クラス • ghci> succ Monday • Tuesday • ghci> pred Saturday • Friday • ghci> [Thursday .. Sunday] • [Thursday,Friday,Saturday,Sunday]
  20. 20. 7.6 型シノニム • type String = [Char] • type AssocList k v = [(k, v)] • data Either a b = Left a | Right b deriving (Eq, Ord, Read, Show)

×