- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
map :: (a -> b) -> [a] -> [b]
fmap :: Functor f => (a -> b) -> f a -> f b
liftM :: Monad m => (a -> b) -> m a -> m b
为什么我们有三个不同的函数,但本质上是做同样的事情?
最佳答案
map
的存在是为了简化列表上的操作以及出于历史原因(请参阅 What's the point of map in Haskell, when there is fmap? )。
You 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
之所以存在,是因为 monad 在 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 的 map
和 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.
关于list - 为什么我们有map、fmap和liftM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7463500/
下面是我正在测试的示例代码 import Control.Exception safeLoad :: FilePath -> IO (Either IOException String) safeLo
下面是我正在测试的示例代码 import Control.Exception safeLoad :: FilePath -> IO (Either IOException String) safeLo
我正在使用 GHCi(版本 6.12.3)和 Haskell 一起玩。我最近阅读了有关仿函数和应用仿函数的文章,我想如果你不能找到类似于 的东西的话。应用仿函数的数量只能使用仿函数的原语来实现。经过
我浏览了一篇文章( http://comonad.com/reader/2012/abstracting-with-applicatives/ )并在那里找到了以下代码片段: newtype Comp
fmap.fmap允许我们“深入两层”进入仿函数: fmap.fmap :: (a -> b) -> f (g a) -> f (g b) 这也适用于应用仿函数吗?假设我想合并 Just (+5)和
我发现自己越来越频繁地做这样的事情...... 我有一个函数 f::IO [a] 然后我想对其应用一个 g::a -> b 类型的函数,以获得 IO [b]。 还有很长的路要走: x x 现在我更愿
我正在阅读《Programming in Haskell》第二版,我看到了这句话: ... there is only one way to make any given parameterised
模式解析错误:f。克 我是初学者,哪里错了? (f . g) x = f (g x) class Functor f where fmap :: (a -> b) -> f a ->
我正在阅读精彩的文章 Haskell Programming from first principles,这是我一生中最开心的时光。我得到了以下我无法拆开的示例(第 1286 页电子阅读器): Pre
我不明白这个简单的表达式类型在 Haskell 中如何检查 (fmap.fmap) sum Just [1, 2, 3] fmap 的组合类型为: fmap.fmap :: (Functor f1
在 ghci 我可以这样做: ghci> (fmap . const) 5 [1,2,3,4,5] [5,5,5,5,5] 但如果我尝试提取子表达式 (fmap . const)进入一个变量我得到一个
我希望拥有无穷无尽的随机或不确定的数字。我继续这样编程: supply :: Monad m => (Int -> m Int) -> m [Int] supply action = sequence
我知道括号会强制执行不同的操作顺序,但我不太明白第一个结果: >> (fmap length Just) [1, 2, 3] 1 而以下内容非常有意义 - 我们将长度函数提升到 Just 结构上,因此
如果我组成两个 fmap Prelude> :t (fmap.fmap) (fmap.fmap) :: (Functor f, Functor f1) => (a -> b) -> f1 (f a
所以假设我想定义一个包含函数的新类型: newtype Test m a = Test(m -> (a, m)) 这可以用来容纳某种状态。 现在假设我想为这个新类型实现 fmap。 instance
考虑以下包装器: newtype F a = Wrap { unwrap :: Int } 我想反驳(作为一个练习,让我的头脑围绕 this interesting post )有一个合法的 Func
给定以下类型族(应该反射(reflect)同构 A×1 ≅ A) type family P (x :: *) (a :: *) :: * where P x () = x P x a =
Haskell的Prelude中是否存在这样的事情? wfmap :: Functor f => a -> (a -> b) -> (b -> a) -
我想更改所有文字。 data Expressions a = ListExpression String[Expressions a] | BinaryExpression String (Expre
我正在尝试理解一些 Haskell 代码。 这是有道理的。 Prelude> fmap (+1) (Just 1) Just 2 这也是有道理的。 Prelude> (fmap.fmap) (+1)
我是一名优秀的程序员,十分优秀!