Skip to content

Notes for Week 13 Applicative Functors Part 2 Applicative Functors

learnhaskell-brisbane edited this page Feb 29, 2012 · 6 revisions

Links:

Exercises:

Exercise 1

* What's the difference between Functors and Applicative Functors ?
* What's the difference between $ and <$> ?

Exercise 2

Demonstrate using an IO action as an Applicative Functor.

Exercise 3

Make the following Tree type an instance of Applicative:

    data Tree a = Node (Tree a) (Tree a) | Leaf a deriving (Show, Eq)

Write a test that proves the following law: 

    pure f <*> x = fmap f x

Exercise 4

Write calcTax, a function that takes 2 Maybes, the first Maybe contains an amount, the second contains a taxation function. 
Return a Maybe with the result of applying the taxation function to the value.

    testCalcTax = calcTax (Just 1000.00) (Just (/10)) == Just 100.00
    
Then write applyTax, same signatures as calcTax but returns the result of subtracting the taxation from the input amount.

	testApplyTax = applyTax (Just 1000.00) (Just (/10)) == Just 900.00

Exercise 5

Write 3 Applicative alternative functions (concatPure, concatFmap, concatDollar) for the following function:

concat :: Maybe String -> Maybe String -> Maybe String
concat (Just x) (Just y) = Just (x ++ y)

Test functions:
---------------
testConcatPure   = concatPure (Just "Yes") (Just "No") == (Just "YesNo")
testConcatFmap   = concatFmap (Just "Yes") (Just "No") == (Just "YesNo")
testConcatDollar = concatDollar (Just "Yes") (Just "No") == (Just "YesNo")

Questions raised during the group:

//TODO

Answers to Exercises:

//TODO

Interesting Asides

//TODO
Clone this wiki locally