Skip to content

Instantly share code, notes, and snippets.

@ethagnawl
Last active October 16, 2016 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ethagnawl/82696f8a4d3ba028b19652e7a864899d to your computer and use it in GitHub Desktop.
Save ethagnawl/82696f8a4d3ba028b19652e7a864899d to your computer and use it in GitHub Desktop.
Applicative Notes - from Graham Hutton's _Programming in Haskell_
-- 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