<$> 有什么用?在 Haskell 中是什么意思?
问题描述:
在阅读一段 Haskell 代码时,我发现了这个:<$>
.它在 Haskell 中是什么意思?经过一些谷歌搜索后,我仍然一无所知.
While reading a piece of Haskell code I came upon this: <$>
. What does it mean in Haskell? After some google searches I remain in the dark.
答
Google 不是 Haskell 的最佳搜索引擎.试试 Hoogle 或 Hayoo†,两者都会马上指出你到这个:
Google is not the best search engine for Haskell. Try Hoogle or Hayoo†, both will point you right away to this:
(<$>) :: Functor f => (a->b) -> f a -> f b
它只是的中缀同义词fmap
,所以你可以写例如
It's merely an infix synonym for fmap
, so you can write e.g.
Prelude> (*2) <$> [1..3]
[2,4,6]
Prelude> show <$> Just 11
Just "11"
像大多数中缀函数一样,它不是内置语法,只是一个函数定义.但是函子是一个如此基本的工具,以至于 <$>
几乎无处不在.
Like most infix functions, it is not built-in syntax, just a function definition. But functors are such a fundamental tool that <$>
is found pretty much everywhere.
†Hayoo 离线有一段时间了.