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.
-
22This isn't the last "weird Haskell symbol" you will come across :)– jub0bsMay 17, 2016 at 21:33
-
5FYI symbolhound (symbolhound.com/?q=%3C%24%3E+haskell) or more specifically hoogle (haskell.org/hoogle/?hoogle=%3C%24%3E) are useful in this situation.– ronMay 18, 2016 at 5:26
-
7It means money, more or less!– dfeuerSep 10, 2019 at 16:29
1 Answer
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.
-
75The top result for "Haskell <$>" on Google is now this page, so in a sense, Google is now a perfectly good search engine in this case! Nov 19, 2018 at 16:05
-
17I like that you used the dagger symbol to denote that Hayoo has been dead for some time. May 9, 2020 at 18:46
-
2Why
<$>
instead of just`fmap`
? Backticks make things infix, and it's easier to read– RichardAug 14, 2022 at 17:39 -
5@Richard there's certainly a point to be made that Haskellers tend to get a bit silly with all those hieroglyphic infix operators. However,
<$>
is so extremely common that one quickly gets the hang of it, it fits mnemonically together with the also very common$
, and it is a bit more compact and visually lean than`fmap`
. I disagree the latter is easier to read in larger contexts. Aug 14, 2022 at 21:34