How fmap works for a list

Find out that you are using haskell , provides a description of the cool Functor class.

I see that for the list it is implemented as follows:

instance Functor [] where fmap = map 

But how does it work?

In the Functor program class, fmap does not even have an implementation. All he has is just a declaration like:

 class Functor f where fmap :: (a -> b) -> fa -> fb 

Just by using a type declaration, how does Haskell correctly calculate the display operation for lists?

+4
source share
1 answer

map is just a normal function with type (a -> b) -> [a] -> [b] . Unlike fmap , it is not part of the Functor class. It works exactly the way you think.

The idea of ​​creating classes is that you use types to figure out which implementation to use. When we say instance Functor [] where ... , we tell the compiler what the fmap implementation for [] (a list type).

In this case, the implementation for fmap is just map , which is a normal function.

+14
source

All Articles