Skip to content

Latest commit

 

History

History
168 lines (151 loc) · 2.77 KB

ch17.adoc

File metadata and controls

168 lines (151 loc) · 2.77 KB

17 Applicative

17.5 Applicative in use

Short Exercises

  1. add <$>

    link:ch17_17.5_1.hs[role=include]
  2. add <$> and <*>, longer version of liftA2

    link:ch17_17.5_2.hs[role=include]
  3. add <$> and <*>, longer version of liftA2

    link:ch17_17.5_3.hs[role=include]
  4. (Maybe Int, Maybe Int) → Maybe Int using Sum? dunno

    Warning
    something is wrong here
    link:ch17_17.5_4.hs[role=include]

Identity Exercise

link:ch17_17.5_5.hs[role=include]

Constant Exercise

Warning
euuuuh, what?
link:ch17_17.5_6.hs[role=include]

Maybe Applicative Exercise

  1. const <$> Just "Hello" <*> pure "World" - pure in the context turns into Maybe

  2. (,,,) <$> Just 90 <*> Just 10 <*> Just "Tierness" <*> pure [1, 2, 3] - all has to be in Maybe, and me fmap to start with

17.8 ZipList Monoid

link:ch17_17.5_7.hs[role=include]

List Applicative Exercise

Warning
had to make somehow "limited" arbitrary, the infinite arbitrary is arbitrary = Cons <$> arbitrary <*> arbitrary and commented out
link:ch17_17.5_8.hs[role=include]

Exercise

Note
the tests are taking a long time, compared to tests done so far
link:ch17_17.5_9.hs[role=include]

Exercise

Sum and Validation Applicative

link:ch17_17.5_10.hs[role=include]

17.9 Chapter Exercises

Warning
doing something wrong here, what do mean "specialize and test in REPL"? some hint, example?
  1. []

    (pure :: (a -> [] a)) 4
    [4]
    
    ((<*>) :: [] (a -> b) -> [] a -> [] b) [(+1)] [1]
    [2]
  2. IO

    (pure :: a -> IO a) "sdf"
    "sdf"
    
    ((<*>) :: IO (a -> b) -> IO a -> IO b) (return (+2)) (return 4)
    6
  3. (,) a

    ((pure :: Monoid b => a -> ((,) b) a) 4) :: (String, Int)
    ("",4)
    
    ((<*>) :: Monoid c => (,) c (a -> b) -> (,) c a -> (,) c b) ("sdf", (+4)) ("sdf", 7)
    ("sdfsdf",11)
  4. (→) e

    pure :: a -> ((->) e) a
    (<*>) :: ((->) e) (a -> b) -> ((->) e) a -> ((->) e) b

For all of those, you need also deriving Eq so you can use quickcheck

  1. newtype Identity a = Identity a deriving Show

    link:ch17_17.9_1.hs[role=include]
  2. data Pair a = Pair a a deriving Show

    link:ch17_17.9_2.hs[role=include]
  3. data Two a b = Two a b

    link:ch17_17.9_3.hs[role=include]
  4. data Three a b c = Three a b c

    link:ch17_17.9_4.hs[role=include]
  5. data Three' a b = Three' a b b

    link:ch17_17.9_5.hs[role=include]
  6. data Four a b c d = Four a b c d

    link:ch17_17.9_6.hs[role=include]
  7. data Four' a b = Four' a a a b

    link:ch17_17.9_7.hs[role=include]

Combinations

neat, but not sure how I would put some condition in

link:ch17_17.9_8.hs[role=include]