b) -> f a -> f b and I want t...">

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?

+3


source to share


2 answers


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

      

+8


source


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]

      

+7


source







All Articles