list – 为什么我们有map,fmap和liftM?

map :: (a -> b) -> [a] -> [b]

fmap :: Functor f => (a -> b) -> f a -> f b

liftM :: Monad m => (a -> b) -> m a -> m b

为什么我们有三个不同的功能,基本上做同样的事情?

地图存在,以简化对列表的操作和历史原因(见What’s the point of map in Haskell, when there is fmap?)。

3You might ask why we need a separate map function. Why not just do away with the current
list-only map function, and rename fmap to map instead? Well, that’s a good question. The
usual argument is that someone just learning Haskell, when using map incorrectly, would much
rather see an error about lists than about Functors.

Typeclassopedia,第20页

fmap和liftM存在,因为monads不是自动函数在Haskell(即使他们应该是):

The fact that we have both fmap and liftM is an
unfortunate consequence of the fact that the Monad type class does not require
a Functor instance, even though mathematically speaking, every monad is a
functor. However, fmap and liftM are essentially interchangeable, since it is
a bug (in a social rather than technical sense) for any type to be an instance
of Monad without also being an instance of Functor.

Typeclassopedia,第33页

编辑:agustuss的地图和fmap的历史:

That’s not actually how it happens. What happened was that the type of map was generalized to cover Functor in Haskell 1.3. I.e., in Haskell 1.3 fmap was called map. This change was then reverted in Haskell 1.4 and fmap was introduced. The reason for this change was pedagogical; when teaching Haskell to beginners the very general type of map made error messages more difficult to understand. In my opinion this wasn’t the right way to solve the problem.

What’s the point of map in Haskell, when there is fmap?

http://stackoverflow.com/questions/7463500/why-do-we-have-map-fmap-and-liftm

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:list – 为什么我们有map,fmap和liftM?