5 ms·
Also fmap :: (Functor f) => (a -> b) -> f a -> f b fmap f (Just x) = Just (f x) So fmap (\x -> x * x) (Just 4) => Just 16 Am I correct in
by tkahn6 16y ago
Also
fmap :: (Functor f) => (a -> b) -> f a -> f b
fmap f (Just x) = Just (f x)
So
fmap (\x -> x * x) (Just 4)
=> Just 16
Am I correct in saying that `fmap` is a functor which lifts the first argument `f` into the monad and then applies it to the second argument?
- johnbender 16y agoAll Monads are, in theory if not in practice, Functors (there happens to be an instance of Functor defined for Maybe). fmap is not a functor but an arrow, in this case a function, that lifts f into the context of the Functor.
- tkahn6 16y agoWould something like lift f = (return . f) Be a functor? [Edit: It says on Wikipedia that a functor maps a function in one category to a function in another category]
- jewbacca 16y agoWARNING: This is the blind leading the blind. I was already well-upvoted for my previous post before realizing (thanks to you, tkahn) that my definition of bind was actually fmap (I think). Crying out for authority here; help us out HN. --- You are not, strictly/pedantically speaking, because fmap is the single function that defines a Functor to begin with [1]: class Functor f where fmap :: (a -> b) -> f a -> f b and Monads are a special case of Functors; so, as the defining relation of a Functor, it doesn't a priori have anything to do with Monads. You are right in seeing that fmap is lifting -- liftM, which is defined in terms of just bind and return for Monads, is (I believe... see warning above) identically fmap. fmap = map for Lists as seen as Functors, for example, which lifts a function (a -> b) to ([a] -> [b])... "into" the Functor/Monad, I suppose. --- [1] http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/src/GHC-Base.html#Functor http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4...