Skip to content

Latest commit

 

History

History
242 lines (215 loc) · 4.2 KB

ch16.adoc

File metadata and controls

242 lines (215 loc) · 4.2 KB

16 Functor

16.4 Let’s talk about f , baby

Intermission: Exercises

because kind of (→) is (→) :: * → * → * the following is

  1. kind of a if *

  2. b has kind * → *, T has kind * → *

  3. c has kind * → * → *

16.7 Commonly used functors

The functors are stacked and that’s a fact

little write out of what happens when we compose functors

replaceWithP = const 'p'

:t replaceWithP
replaceWithP :: b -> Char

lms = [Just "Ave", Nothing, Just "woohoo"]

now we try try to this:
(fmap . fmap) replaceWithP lms
and the result is:
[Just 'p',Nothing,Just 'p']

let's disect this:

:t (fmap . fmap)
(Functor f, Functor f1) => (a -> b) -> f (f1 a) -> f (f1 b)

fmap :: Functor f => (m -> n) -> f m -> f n

fmap :: Functor g => (x -> y) -> g x -> g y


(x -> y) -> g x -> g y
apply: replaceWithP :: b -> Char
result is:
g b -> g Char
that is (b -> Char) substitued in (g x -> g y)

(m -> n) -> f m -> f n
apply: (g b -> g Char)
result is:
f (g b) -> f (g Char)
that is (b -> Char) substitued in (f m -> f n)


f (g b) -> f (g Char)
apply: lms
result is:
f unwraps the list
g unwraps the Maybe
replaceWithP is applied to the value

voilà, it does work as advertised :)

three compositions go one more level deeper
(fmap . fmap . fmap)
as we can also fmap over String

One more round for the P-Funkshun

link:ch16_16.7_0.hs[role=include]

Intermission: Lifting Exercises

  1. a = fmap (+1) (read "[1]" :: [Int])

  2. b = (fmap . fmap) (++ "lol") (Just ["Hi,", "Hello"])

  3. c = fmap (*2) (\x → x - 2) also c = (*2) . (\x → x - 2)

  4. d = fmap ((return '1' ++) . show) (\x → [x, 1..3])

    also d = ((return '1' ++) . show) . (\x → [x, 1..3])

    and without return that I do not undestand here

    d = fmap (("1" ++) . show) (\x → [x, 1..3])

  5. e

    Warning
    WTF? have no clue

16.10 Intermission: Exercises

  1. newtype Identity a = Identity a

    link:ch16_16.10_1.hs[role=include]
  2. data Pair a = Pair a a

    link:ch16_16.10_2.hs[role=include]
  3. data Two a b = Two a b

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

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

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

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

    link:ch16_16.10_7.hs[role=include]
  8. data Trivial = Trivial has kind *, cannot be Functor

16.11 Ignoring possibilities

Maybe - Short Exercise

link:ch16_16.11_1.hs[role=include]

Either - Short Exercise

  1. Functor for Either like type

    link:ch16_16.11_2.hs[role=include]
  2. Either has kind * → * → * , you cannot apply "second" argument first to get kind * → *

16.17 Chapter exercises

Is valid Functor

  1. no, kind *

  2. yes, kind * → *

  3. yes, kind * → *, similar to Maybe

  4. yes, Mu f kind is * → *, but making it Functor instance is kinda funky.

  5. no, data D =…​. not a * → *

Rearrange the arguments

  1. flip a and b in the Sum

    link:ch16_16.17_1.hs[role=include]
  2. flip b and c in the Company

    link:ch16_16.17_2.hs[role=include]
  3. flip a and b in the More

    link:ch16_16.17_3.hs[role=include]

Write Functor instances

  1. data Quant a b

    link:ch16_16.17_4.hs[role=include]
  2. simillar to Constant in the 16.12 chapter

    link:ch16_16.17_5.hs[role=include]
  3. Flip from earlier

    link:ch16_16.17_6.hs[role=include]
  4. EvilGoateeConst

    link:ch16_16.17_7.hs[role=include]
  5. virtually identical to Wrap earlier, f in LiftItOut f a needs to be Functor to make sense at all

    link:ch16_16.17_8.hs[role=include]
  6. double fmap wrap

    link:ch16_16.17_9.hs[role=include]
  7. IgnoreOne

    link:ch16_16.17_10.hs[role=include]
  8. Notorious

    link:ch16_16.17_11.hs[role=include]
  9. List

    link:ch16_16.17_12.hs[role=include]
  10. triple deep

    link:ch16_16.17_13.hs[role=include]
  11. looks right, but not sure

    link:ch16_16.17_14.hs[role=include]