Why do we have map, fmap and liftM?

map :: (a -> b) -> [a] -> [b] fmap :: Functor f => (a -> b) -> fa -> fb liftM :: Monad m => (a -> b) -> ma -> mb 

Why do we have three different functions that do almost the same thing?

+84
list functor haskell monads redundancy
Sep 18 '11 at 18:32
source share
1 answer

map exists to simplify operations on lists for historical reasons (see What is the point of a map in Haskell when there is fmap? ).

3You may ask why we need a separate display function. Why not just end the current display of only the list, and instead rename fmap? Well, that’s a good question. the usual argument is that someone just studying Haskell is using the map incorrectly, rather look at the error about lists than about functors.

- Typeclassopedia , p. 20

fmap and liftM exist because monads were not automatically functors in Haskell:

The fact that we have both fmap and liftM is an unfortunate consequence of the fact that a class like Monad does not require an instance of Functor, although mathematically speaking, every monad is a functor. However, fmap and liftM are essentially interchangeable, since a mistake (in the social, not technical sense) for any type that is an instance from the Monad, is not also an instance of Functor.

- Typeclassopedia , p. 33

Edit: history of agustuss map and fmap :

This is not how it happens. It so happened that the map type was generalized to cover Functor in Haskell 1.3. Ie, in Haskell 1.3 fmap was called a map. This change was then returned to Haskell 1.4 and fmap was introduced. The reason for this change was pedagogical; When teaching Haskell to beginners, a very general type of map made it difficult to understand error messages. In my opinion, this was the wrong way to solve the problem.

- What is the point of the map in Haskell when there is fmap?

+79
Sep 18 '11 at 18:40
source share



All Articles