What does "f" mean in the functor fmap function?
I am looking at the following function:
fmap :: (a -> b) -> f a -> f b
and I want to understand what "f" is like in ( f a
or f b
). The article I'm reading describes it as a "field", but what's the real name for it? Is it just a type variable? I think I am confusing it and consider this to be a functional application - which is correct?
Your intuition that these are some kind of app-function is correct, but they are not regular functions. Instead, it is the use of type-level type constructors.
In particular, functors must be of the form (type-to-type) * -> *
, which means that they take one type argument and create a concrete type *
, for example [Int]
.
Examples of such type constructors include IO, Maybe, [], Either e
many others, and all of these specific examples have valid instances of Functor.
fmap (+1) [1,2,3] :: [] Int -- also known as [Int]
= [2,3,4]
fmap (+1) (Just 1) :: Maybe Int
= Just 2
fmap (+1) (Right 1) :: Either e Int
= Right 2
fmap (+1) (return 1) :: IO Int -- Uses Monad IO instance as well
"=" 2
It is a type variable that represents the specific functor you are working on. For example, IO is a functor, so you can specialize fmap
in
fmap :: (a -> b) -> IO a -> IO b
Similarly, you can select it into lists:
fmap :: (a -> b) -> [a] -> [b]