The type of fmap in Functor is:
fmap :: Functor f => (a -> b) -> f a -> f b
it looks like ,first apply function (a -> b) to the parameter of f a to create a result of type b, then apply f to it, and result is f b
using Maybe a
for example:
fmap show (Just 1)
result is : Just "1"
same as saying:
Just (show 1)
but when (->)
is used as a Functor (in Control.Monad.Instances
)
import Control.Monad.Instances
(fmap show Just) 1
result is : "Just 1"
that is, Just
is applied first, then show
is applied. In another example ,result is same:
fmap (*3) (+100) 1
result is 303
why not *3
first, then +100
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…