すごいhaskell読書会#10

38
すごいHaskellたのしく学ぼう! 11. ファンクターからアプリカティブファンクターへ 伊勢 シン / 伊藤 伸裕 すごいHaskell読書会 in #10 @ Fenrir Inc.

Upload: shin-ise

Post on 04-Jul-2015

810 views

Category:

Documents


1 download

TRANSCRIPT

  • 1. Haskell!11. / Haskell in #10 @ Fenrir Inc.

2. doitaka http://d.hatena.ne.jp/doitaka/20130609/1370782111 Applicativepure Functor http://d.hatena.ne.jp/doitaka/20130613/1371146740 http://d.hatena.ne.jp/doitaka/20130618/1371571806 3. 7 7.11 Functor 4. Functor Functorfmap a b a b class Functor f wherefmap :: (a -> b) -> f a -> f b 5. Hoogle Maybe Either a b Either a instance Functor Maybe wherefmap _ Nothing = Nothingfmap f (Just a) = Just (f a)instance Functor (Either a) wherefmap _ (Left x) = Left xfmap f (Right y) = Right (f y) 6. Functor fa fba -> bclass Functor f wherefmap :: (a -> b) -> f a -> f b 7. MaybeMaybea Maybef af :: a -> binstance Functor Maybe wherefmap _ Nothing = Nothingfmap f (Just a) = Just (f a) 8. 7 7 7.11 Functor 9. 7 10. cf. 7https://gist.github.com/yashigani/5231928#-12 11. I/O IOreturn(f result)f :: a -> binstance Functor IO wherefmap f action = doresult ) r a (->) 2(->) r 14. fmap :: (a -> b) -> f a -> f bf (->) rfmap :: (a -> b) -> ((->) r a) -> ((->) r b)fmap :: (a -> b) -> (r -> a) -> (r -> b)r -> a a -> b b . r $ a 15. Control.Monad.Instances 232 16. (lifting) a -> b -> ca (b -> c) fmap (a -> b) -> f a -> f b (a -> b) -> (f a -> f b) 17. (lifting) ghci> :set -XNoMonomorphismRestriction ghci> let shout = fmap (++"!")ghci> :t shoutshout :: Functor f => f [Char] -> f [Char] ghci> shout ["ha","ka","ta","no"]["ha!","ka!","ta!","no!"]f a -> f b 18. 2 1 id 2 f g g f fmap (f . g) x = fmap f (fmap g x) Haskell Functor Functor 19. 1 Just id Nothing Nothing Just x fmap id (Just x)Just (id x)Just xJust x 20. CMaybe fmap counter fmap + 1 f id CJust counter x CJust (counter + 1) x 1 data CMaybe a = CNothing | CJust Int a deriving (Show)instance Functor CMaybe wherefmap f CNothing = CNothingfmap f (CJust counter x) = CJust (counter+1) (f x) 21. (1) fmap f 2ghci> :t fmap (++) (Just "hey")fmap (++) (Just "hey") :: Maybe ([Char] -> [Char])ghci> :t fmap compare (Just a)fmap compare (Just a) :: Maybe (Char -> Ordering) ghci> :t fmap compare "A LIST OF CHARS"fmap compare "A LIST OF CHARS" :: [Char -> Ordering] ghci>:t fmap(x y z -> x + y / z)[3,4,5,6]fmap (x y z -> x + y / z) [3,4,5,6] :: (Fractional a) => [a -> a -> a] 22. (2) fmap Just (3 *) Just 5 Just 15 ghci> let a = fmap (*) [1,2,3,4] ghci> :t aa :: [Integer -> Integer] ghci> fmap (f -> f 9 ) a [9,18,27,36]ghci> fmap ($9) a [9,18,27,36] 23. (3) Control.Applicative pure f Just 15 Functor fmap class (Functor f) => Applicative f where pure:: a -> f a () :: f (a -> b) -> f a -> f b 24. Maybe Maybe Applicative Hoogle pure () Just Nothing Nothing Just f 2 fmap f 2fmap 2instance Applicative Maybe where pure = JustNothing _ = Nothing(Just f) something = fmap f something 25. Maybe Maybe Just = pure ghci> Just (+3) Just 9 Just 12ghci> pure (+3) Just 10 Just 13ghci> pure (+3) Just 9Just 12ghci> Just (++"hahah") NothingNothingghci> Nothing Just "woot" Nothingfmap (+3) (Just 9)fmap (+3) (Just 10)fmap (+3) (Just 9)fmap (++"hahah") Nothingfmap Nothing (Just "woot") 26. pure (+) Just 3 Just 5Just ((+) 3) Just 5Just ((+) 3 5)Just 8 27. pure f x = fmap f x = f x pure f x fmap f x pure f x y fmap f x y Control.Applicative fmap f x f x ghci> (++) Just "johntra" Just "volta" Just "johntravolta" 28. Applicative pure 1instance Applicative [] where pure x = [x]fs xs = [f x | f a ghci> (+) (+3) (*100) $ 5508 35. ZipList [(+3),(*2)] [1,2] [3+1, 3+2, 2*1, 2*2] [4,5,2,4] [1+3, 2*2] [4, 4] ZipListinstance Applicative ZipList where pure x = ZipList (repeat x)ZipList fs ZipList xs = ZipList (zipWith (f x -> f x) fs xs 36. ZipList ZipListShowgetZipListghci> getZipList $ (+) ZipList [1,2,3] ZipList [100,100,100] [101,102,103]ghci> getZipList $ (+) ZipList [1,2,3] ZipList [100,100..] [101,102,103]ghci> getZipList $ max ZipList [1,2,3,4,5,3] ZipList [5,3,1,2] [5,3,3,4]ghci> getZipList $ (,,) ZipList "dog" ZipList "cat" ZipList "rat"[(d,c,r),(o,a,a),(g,t,t)] 37. pure f x = fmap f x pure id v = v pure (.) u v w = u (v w) pure f pure x = pure (f x) u pure y = pure ($ y) u pure f x = fmap f x 38.