Your SlideShare is downloading. ×
すごい Haskell 読書会 in 大阪 #5
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

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

300
views

Published on

http://atnd.org/events/36770

http://atnd.org/events/36770


0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
300
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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