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
-
a = fmap (+1) (read "[1]" :: [Int])
-
b = (fmap . fmap) (++ "lol") (Just ["Hi,", "Hello"])
-
c = fmap (*2) (\x → x - 2)
alsoc = (*2) . (\x → x - 2)
-
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 hered = fmap (("1" ++) . show) (\x → [x, 1..3])
-
e
WarningWTF? have no clue
-
newtype Identity a = Identity a
link:ch16_16.10_1.hs[role=include]
-
data Pair a = Pair a a
link:ch16_16.10_2.hs[role=include]
-
data Two a b = Two a b
link:ch16_16.10_3.hs[role=include]
-
data Three a b c = Three a b c
link:ch16_16.10_4.hs[role=include]
-
data Three' a b = Three' a b b
link:ch16_16.10_5.hs[role=include]
-
data Four a b c d = Four a b c d
link:ch16_16.10_6.hs[role=include]
-
data Four' a b = Four' a a a b
link:ch16_16.10_7.hs[role=include]
-
data Trivial = Trivial
has kind*
, cannot be Functor
-
no, kind
*
-
yes, kind
* → *
-
yes, kind
* → *
, similar toMaybe
-
yes,
Mu f
kind is* → *
, but making it Functor instance is kinda funky. -
no,
data D =….
not a* → *
-
flip
a
andb
in theSum
link:ch16_16.17_1.hs[role=include]
-
flip
b
andc
in theCompany
link:ch16_16.17_2.hs[role=include]
-
flip
a
andb
in theMore
link:ch16_16.17_3.hs[role=include]
-
data Quant a b
link:ch16_16.17_4.hs[role=include]
-
simillar to
Constant
in the 16.12 chapterlink:ch16_16.17_5.hs[role=include]
-
Flip
from earlierlink:ch16_16.17_6.hs[role=include]
-
EvilGoateeConst
link:ch16_16.17_7.hs[role=include]
-
virtually identical to
Wrap
earlier,f
inLiftItOut f a
needs to beFunctor
to make sense at alllink:ch16_16.17_8.hs[role=include]
-
double fmap wrap
link:ch16_16.17_9.hs[role=include]
-
IgnoreOne
link:ch16_16.17_10.hs[role=include]
-
Notorious
link:ch16_16.17_11.hs[role=include]
-
List
link:ch16_16.17_12.hs[role=include]
-
triple deep
link:ch16_16.17_13.hs[role=include]
-
looks right, but not sure
link:ch16_16.17_14.hs[role=include]