Last active
October 16, 2016 01:19
-
-
Save ethagnawl/82696f8a4d3ba028b19652e7a864899d to your computer and use it in GitHub Desktop.
Applicative Notes - from Graham Hutton's _Programming in Haskell_
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- the class of functors which support <*> and pure are called | |
-- applicative functors or applicatives | |
-- class Functor f => Applicative f where | |
-- pure :: a -> f a | |
-- <*> :: f (a -> b) -> f a -> f b | |
-- generalise the idea of mapping to functions with multiple arguments | |
-- associates to the left | |
-- ((g <*> x) <*> y) <*> z | |
import Control.Applicative | |
instance Applicative [] where | |
pure x = [x] | |
fs <*> xs = [f x | f <- fs, x <- xs] | |
[(+),(*)] <*> [10] <*> [10] -- [20, 100] | |
liftA2 (*) [10] [10] -- [100] | |
let func x y z = x * y * z | |
Just func <*> Just 1 <*> Just 10 <*> Just 100 -- Just 1000 | |
pure (Just 10) -- Just 10 | |
pure func <*> Just 1 <*> Just 10 <*> Just 100 -- Just 1000 | |
liftA3 func (Just 1) (Just 10) (Just 100) -- Just 1000 | |
func <$> Just 1 <*> Just 10 <*> Just 100 -- Just 1000 | |
Just (.) <*> Just (+ 1) <*> Just (* 2) <*> Just 10 -- 1 + (2 * 10) | |
Just (.) <*> Just (+ 1) <*> Just (* 2) <*> Just 10 | |
func <$> Just 1 <*> Just 10 <*> Just 100 -- Just 1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment