< $> 在哈斯克尔意味着什么?

在阅读一段 Haskell 代码时,我偶然发现了这个: <$>。这在哈斯克尔意味着什么?经过一些谷歌搜索我仍然在黑暗中。

34964 次浏览

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

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 has been offline for quite a while now.