Upgrade to Pro — share decks privately, control downloads, hide ads and more …

sugoih 8

sugoih 8

すごいHaskell読書会 in 大阪 2週目 #8 : ATND
https://atnd.org/events/53833

USAMI Kosuke

August 20, 2014
Tweet

More Decks by USAMI Kosuke

Other Decks in Programming

Transcript

  1. ʢલฤͷ͓͞Β͍ʣ ৽͍͠σʔλܕΛఆٛ͢Δ data ܕ໊ = ஋ίϯετϥΫλ data Bool = False

    | True data Shape = Circle Float Float Float | Rectangle Float Float Float Float data Point = Point Float Float
  2. ʢલฤͷ͓͞Β͍ʣ Ϩίʔυߏจ data Person = Person { firstName :: String

    , lastName :: String , age :: Int , height :: Float , phoneNumber :: String , flavor :: String }
  3. ʢલฤͷ͓͞Β͍ʣ ܕҾ਺ data ܕίϯετϥΫλ = ஋ίϯετϥΫλ ʢܕ໊ ܕҾ਺ʣ data Maybe

    a = Nothing | Just a data Either a b = Left a | Right b -- ̏࣍ݩϕΫτϧͷྫ data Vector a = Vector a a a
  4. ʢલฤͷ͓͞Β͍ʣ Πϯελϯεͷࣗಈಋग़ data Day = Monday | Tuesday | Wednesday

    | Thursday | Friday | Saturday | Sunday deriving (Eq, Ord, Show, Read, Bounded, Enum) -- ܕΫϥεͷ࣮૷͕ิΘΕɺҎԼ͕ՄೳʹͳΔ ghci> Saturday == Sunday False ghci> show Wednesday Wednesday
  5. ʢલฤͷ͓͞Β͍ʣ ܕγϊχϜ type String = [Char] type Name = String

    type PhoneNumber = String type PhoneBook = [(Name, PhoneNumber)] -- ҎԼͷྫͰ͸ AssocList ͸ܕίϯετϥΫλ type AssocList k v = [(k, v)]
  6. ྫɿಠࣗϦετܕ data List a = Empty | Cons a (List

    a) deriving (Show, Read, Eq, Ord) -- Ϩίʔυߏจͷ৔߹ data List a = Empty | Cons { listhead :: a , listtail :: List a } deriving (Show, Read, Eq, Ord)
  7. ಠࣗϦετܕͷϦετදݱ 4 ઌͷಠࣗఆٛͷ Cons ͸ඪ४ͷ : ʹ͋ͨΔ΋ͷ Empty -- []

    ʹ͋ͨΔ 5 `Cons` Empty -- 5:[] ʹ͋ͨΔ 4 `Cons` (5 `Cons` Empty) -- 4:5:[] ʹ͋ͨΔ 3 `Cons` (4 `Cons` (5 `Cons` Empty)) -- 3:4:5:[] ʹ͋ͨΔ
  8. վળ݁Ռ 4 ϦετΛ͜͏ॻ͚ΔΑ͏ʹͳͬͨ 5 :-: Empty -- 5 `Cons` Empty

    4 :-: 5 :-: Empty -- 4 `Cons` (5 `Cons` Empty) 3 :-: 4 :-: 5 :-: Empty -- 3 `Cons` (4 `Cons` (5 `Cons` Empty))
  9. ྫɿಠࣗϦετܕͷ݁߹ infixr 5 ^++ (^++) :: List a -> List

    a -> List a Empty ^++ ys = ys (x :-: xs) ^++ ys = x :-: (xs ^++ ys) 4 ࣮͸ύλʔϯϚονͱ͸ɺ஋ίϯετϥΫλͷϚον 4 ඪ४ͷ : ΋஋ίϯετϥΫλ
  10. ผͷྫɿೋ෼୳ࡧ໦ data Tree a = EmptyTree | Node a (Tree

    a) (Tree a) deriving (Show) 4 ೋ෼୳ࡧ໦ɿ֤ཁૉʹ͍ͭͯɺ 4 ࠨ෦෼໦ͷશཁૉ͕ͦͷཁૉΑΓখ͍͞ 4 ӈ෦෼໦ͷશཁૉ͕ͦͷཁૉΑΓେ͖͍
  11. ໦ʹཁૉΛ௥Ճ͢Δؔ਺ 4 ༩͑ΒΕͨ໦ͷ௚઀ߋ৽͸ෆՄɺ৽͍͠໦Λ࡞ͬͯฦ͢ singleton :: a -> Tree a singleton

    x = Node x EmptyTree EmptyTree treeInsert :: (Ord a) => a -> Tree a -> Tree a treeInsert x EmptyTree = singleton x treeInsert x (Node y left right) | x == y = Node y left right -- ಉ͡ͳΒૠೖ͠ͳ͍ | x < y = Node y (treeInsert x left) right | x > y = Node y left (treeInsert x right)
  12. ໦ͷੜ੒ ghci> let nums = [8,6,4,1,7,3,5] ghci> let numsTree =

    foldr treeInsert EmptyTree nums 4 tree1 = treeInsert 5 EmptyTree 4 tree2 = treeInsert 3 tree1 4 tree3 = treeInsert 7 tree2 4 ... 4 numsTree = treeInsert 8 tree7
  13. ໦ʹཁૉ͕ଐ͢Δ͔ௐ΂Δؔ਺ treeElem :: (Ord a) => a -> Tree a

    -> Bool treeElem x EmptyTree = False treeElem x (Node y left right) | x == y = True | x < y = treeElem x left | x > y = treeElem x right
  14. ܕΫϥεதڃߨ࠲ 4 ܕΫϥεͷ෮श 4 ͋Δܕ T ͕͋ΔܕΫϥε C ͷΠϯελϯε =

    ܕ T ʹରͯ͠ܕΫϥε C ͕ఆٛ͢Δؔ਺Λ࢖͑Δ 4 ܕΫϥεΛࣗ෼Ͱ࡞Δʹ͸ʁ
  15. Eq ܕΫϥεͷએݴ class Eq a where (==) :: a ->

    a -> Bool (/=) :: a -> a -> Bool x == y = not (x /= y) x /= y = not (x == y) 4 a ͸ܕҾ਺ͰɺEq ͷΠϯελϯεͱͳΔܕ
  16. Eq ܕΫϥεͷΠϯελϯεએݴ data TrafficLight = Red | Yellow | Green

    instance Eq TrafficLight where Red == Red = True Yellow == Yellow = True Green == Green = True _ == _ = False 4 ஫ҙɿ௨ৗ͸ deriving Ͱࣗಈಋग़͢Δ
  17. ࠷খ׬શఆٛ x == y = not (x /= y) x

    /= y = not (x == y) 4 Eq ͷΠϯελϯε͸ == ͔ /= ͷͲͪΒ͔Λఆٛ͢Δඞཁ͕ ͋Δ 4 ยํΛఆٛʢσϑΥϧτ࣮૷Λ্ॻ͖ʣ͢Ε͹ɺ΋͏ยํ ΋ఆٛ͞ΕΔ
  18. Maybe Λ Eq ͷΠϯελϯεʹ͢ Δʁ class Eq a where (==)

    :: a -> a -> Bool 4 ܕҾ਺ a ͸۩ମܕͰͳͯ͘͸μϝʢؔ਺ఆٛʹ͋Θͳ͍ʣ 4 Maybe ͸۩ମܕͰͳ͘ଟ૬ܕʢܕίϯετϥΫλʣ 4 ͡Ό͋۩ମܕ Maybe Char Λ Eq ͷΠϯελϯεʹ͢Δʁ 4 ͦΕͰ͸ Maybe Int ͸ʁ ɾɾɾ͖Γ͕ͳ͍
  19. ଟ૬ܕΛܕΫϥεͷΠϯελϯεʹ ͢Δ 4 ΠϯελϯεએݴʹܕҾ਺Λ͚ͭΔ instance Eq (Maybe m) where Just

    x == Just y = x == y Nothing == Nothing = True _ == _ = False 4 ஫ҙɿ͜ͷએݴ͸ෆे෼ʢ࣍ϖʔδࢀরʣ
  20. ΠϯελϯεએݴʹܕΫϥε੍໿Λ ͚ͭΔ 4 ܕ m ͕ Eq ܕΫϥεͰͳ͍ͱμϝ 4 ܕΫϥε੍໿Λ͚ͭΔ

    instance (Eq m) => Eq (Maybe m) where Just x == Just y = x == y Nothing == Nothing = True _ == _ = False
  21. ࠶౓੔ཧͯ͠ΈΔ 4 Eq ܕΫϥεએݴͰ͸ɺa ͕۩ମܕͰͳ͍ͱμϝ 4 (==) :: a ->

    a -> Bool ͱ࢖ΘΕΔ͔Β 4 a ʹ Maybe ͸౉ͤͳ͍ 4 (==) :: Maybe -> Maybe -> Bool ͸μϝ 4 a ʹ Maybe m ͳΒ౉ͤΔ 4 (==) :: (Eq m) => Maybe m -> Maybe m -> Bool
  22. Yes ͱ No ͷܕΫϥε 4 Haskell Ͱ͸ਅཧ஋͕ඞཁͳՕॴͰ͸ݫີʹ Bool ܕΛ࢖͏ 4

    ଞͷݴޠͰ͸ͦ͏Ͱͳ͍΋ͷ΋͋ΔɿJavaScript ͷྫ 4 if (0) 4 if ("") 4 if (false) 4 ྨࣅͷ͜ͱΛಠࣗͷܕΫϥεͰ࣮ݱͯ͠ΈΔ
  23. ܕΫϥεએݴͱΠϯελϯεએݴ class YesNo a where yesno :: a -> Bool

    instance YesNo Int where yesno 0 = False yesno _ = True instance YesNo [a] where yesno [] = False yesno _ = True
  24. instance YesNo Bool where yesno = id -- ͦͷ··ฦ͢ instance

    YesNo (Maybe a) where yesno Nothing = False yesno (Just _) = True instance YesNo (Tree a) where yesno EmptyTree = False yesno _ = True instance YesNo TrafficLight where yesno Red = False yesno _ = True
  25. if ͷ୅ସ yesnoIf :: (YesNo y) => y -> a

    -> a -> a yesnoIf yesnoVal yesResult noResult = if yesno yesnoVal then yesResult else noResult ghci> yesnoIf [] "YEAH!" "NO!" "NO!" ghci> yesnoIf [2,3,4] "YEAH!" "NO!" "YEAH!"
  26. Functor ܕΫϥε class Functor f where fmap :: (a ->

    b) -> f a -> f b 4 ͜ͷ f ͸۩ମܕͰ͸ͳ͘ɺܕίϯετϥΫλ 4 Ҿ਺̍ : ܕ a ͔Βܕ b ΁ͷؔ਺ܕ 4 Ҿ਺̎ : ܕίϯετϥΫλ f ʹܕҾ਺ a Λద༻ͨ͠ܕ 4 ໭Γ஋ : ܕίϯετϥΫλ f ʹܕҾ਺ b Λద༻ͨ͠ܕ
  27. Ϧετͷ map -- Ϧετͷ map map :: (a -> b)

    -> [a] -> [b] -- Functor ͷ fmap fmap :: (a -> b) -> f a -> f b 4 map ͸ϦετݶఆͰಈ࡞͢Δ fmap ͩͱ͍͑Δ
  28. Ϧετͷ Functor Πϯελϯεએݴ instance Functor [] where fmap = map

    4 ͜͜Ͱɺ[] ͸ܕίϯετϥΫλͰ͋Δ͜ͱʹ஫ҙ 4 [Int] [String] ͳͲ͕۩ମܕ
  29. Maybe ͷ Functor Πϯελϯεએݴ instance Functor Maybe where fmap f

    (Just x) = Just (f x) fmap f Nothing = Nothing 4 Maybe ͸ܕίϯετϥΫλ
  30. ೋ෼୳ࡧ໦ͷ Functor Πϯελϯε એݴ instance Functor Tree where fmap f

    EmptyTree = EmptyTree fmap f (Node x left right) = Node (f x) (fmap f left) (fmap f right) 4 Tree ͸ܕίϯετϥΫλʢTree a ͕۩ମܕʣ 4 ஫ҙɿ೚ҙͷؔ਺Λద༻ͨ͠ޙ͸ೋ෼୳ࡧ໦ͷੑ࣭Λอͨ ͳ͍
  31. Either ͷ৔߹͸ʁ 4 Either ͸ܕҾ਺Λ 2 ͭͱΔܕίϯετϥΫλ 4 Either a

    b ͕۩ମܕ 4 1 ͚ͭͩ෦෼ద༻͢Δͱ Functor ʹͰ͖Δ 4 Either a ͸ܕҾ਺Λ 1 ͭͱΔܕίϯετϥΫλ
  32. Either ͷ Functor Πϯελϯεએݴ instance Functor (Either a) where fmap

    f (Right x) = Right (f x) fmap f (Left x) = Left x 4 data Either a b = Left a | Right b 4 Left ͱ Right Ͱܕ͕ҧ͏͜ͱʹ஫ҙ 4 fmap Ͱద༻͢Δؔ਺ f ͸ b -> c ܕ 4 Right ଆʹ͸ద༻Ͱ͖Δ͕ɺLeft ଆʹ͸ద༻Ͱ͖ͳ͍
  33. Data.Map ͷ৔߹͸ʁʢ࿅श໰୊ʣ 4 Map k v ͸ k ܕ͕Ωʔɺv ܕ͕஋ͷσʔλߏ଄

    4 fmap Ͱద༻͢Δؔ਺͸ v -> v' ܕ 4 ໰୊ɿͲͷΑ͏ʹ Functor ͷΠϯελϯεʹͳΔ͔ʁ 4 MyFunctor ܕΫϥεએݴΛॻ͘ 4 Map ͷ MyFunctor ΠϯελϯεએݴΛॻ͘ 4 Map ʹ myfmap Ͱؔ਺Λద༻͢Δ
  34. ܕͷछྨ ghci> :k Int Int :: * ghci> :k Maybe

    Maybe :: * -> * ghci> :k Maybe Int Maybe Int :: * 4 * ͸۩ମܕΛද͢ه߸ 4 Maybe ͸۩ମܕΛҾ਺ʹͱͬͯ۩ମܕΛฦ͢
  35. Functor ʹͳΕΔܕͷछྨ class Functor f where fmap :: (a ->

    b) -> f a -> f b 4 f a f b ͷछྨ͸ *ʢؔ਺ͷܕએݴʹ࢖ΘΕ͍ͯΔͨΊʣ 4 f ͷछྨ͸ * -> * Ͱͳͯ͘͸ͳΒͳ͍